This site requires JavaScript, please enable it in your browser!
Greenfoot back
student1
student1 wrote ...

2018/4/18

Level Select

1
2
student1 student1

2018/4/18

#
making a game and i want to make it so that when the player collects a certain number of objects it goes to the next level
danpost danpost

2018/4/18

#
student1 wrote...
making a game and i want to make it so that when the player collects a certain number of objects it goes to the next level
Use the act method of the world to check the count of objects the player has collected, if that certain number has been reached, set the new level active.
student1 student1

2018/4/18

#
how would i do that
danpost danpost

2018/4/18

#
If it would make it easier, just ask in the class of the player -- immediately after collecting an object.
student1 student1

2018/4/18

#
i'm guessing it would be if player has collected (lets say 5 coins) then move to next level but i'm unsure how i would put that in to code
danpost danpost

2018/4/18

#
student1 wrote...
i'm guessing it would be if player has collected (lets say 5 coins) then move to next level but i'm unsure how i would put that in to code
How do you think it would be coded?
student1 student1

2018/4/19

#
1
2
3
4
5
6
7
// in the player class
private int coinNum;
// in act method
if(coinNum=5)
{
Greenfoot.setWorld(new next());
}
i'm pretty sure this is wrong but i was never taught how to make it so that it counts how many objects have been collected before it goes to another level
danpost danpost

2018/4/19

#
student1 wrote...
<< Code Omitted >> i'm pretty sure this is wrong but i was never taught how to make it so that it counts how many objects have been collected before it goes to another level
Only thing wrong is int cannot be converted to boolean. The single equal sign always sets a value -- it does not perform any comparison. You need to use two equal signs to compare the int values:
1
if (coinNum == 5)
student1 student1

2018/4/19

#
ok, so how do i make it so that the coin class affects line 2
danpost danpost

2018/4/19

#
student1 wrote...
ok, so how do i make it so that the coin class affects line 2
I cannot say -- I have yet to see your coin class at all or your player class in its entirety.
Yehuda Yehuda

2018/4/19

#
If your player is collecting coins then why can you not just add 1 to coinNum after each collection?
danpost danpost

2018/4/19

#
Yehuda wrote...
If your player is collecting coins then why can you not just add 1 to coinNum after each collection?
This is about going to another level after so many coins have been collected -- not about counting (collecting) the coins.
student1 student1

2018/4/19

#
i am doing this test for a friend so i only have two world classes (myWorld and next) and two actor classes (player and coin) the player class has the code above and code for moving in all directions. i don't have access to greenfoot at the moment so i am wondering if you can tell me if this would work.
1
2
3
4
5
6
7
8
9
10
11
12
13
private int coinNum;
//in act method
Actor coin();
coin = setOneObjectAtOffset(0,0,Coin.class);
if(coin !=null)
{
Greenfoot.removeObject(Coin.class);
coinNum + 1;
}
if(coinNum==5)
{
Greenfoot.setWorld(new next());
}
danpost danpost

2018/4/19

#
student1 wrote...
i am doing this test for a friend so i only have two world classes (myWorld and next) and two actor classes (player and coin) the player class has the code above and code for moving in all directions. i don't have access to greenfoot at the moment so i am wondering if you can tell me if this would work. << Code Omitted >>
Remove the parenthesis from line 3 ("coin" is a variable name here -- not a method call). Change "set" in line 4 to "get" (there is no "setOneObjectAtOffset" method for an Actor object). Change "Greenfoot" to "getWorld" on lines 7 and 12 (getWorld is a World object method -- not a Greenfoot class method). Then it might work.
student1 student1

2018/4/19

#
so this
1
2
3
4
5
6
7
8
9
10
11
12
private int coinNum;
//in act method
coin = getOneObjectAtOffset(0,0,Coin.class);
if(coin !=null)
{
getWorld().removeObject(Coin.class);
coinNum + 1;
}
if(coinNum==5)
{
getWorld().setWorld(new next());
}
There are more replies on the next page.
1
2