I'm tasked with creating a java game in Greenfoot. I would like to collect 5 coins, a door appears, you enter the door, next level. I've done that so far. There's a problem with this if/else statement I think:
if (coinsCollected == 5 && levelCounter == 0) {
getWorld().addObject(new door_temp(), 157, 162);
levelCounter += 1;
coinsCollected = 0;
secondLevel();
}
if (coinsCollected == 5 && levelCounter == 1) {
getWorld().addObject(new door_temp(), 961, 170);
levelCounter += 1;
coinsCollected = 0;
thirdLevel();
}
For some reason, even if I'm on level 2 and I touch the door, the first statement gets executed. I don't understand why though because I increment levelCounter by one each time.
Thanks to anyone that can help ;)
Your statement isn't actually an if/else, so as it stands it will execute both parts when you touch the door on level 0 (assuming secondLevel() returns).
I suspect the problem is that levelCounter is declared as an instance variable in the world class for level 1, but when you move to level 2, the instance variable for that world class is 0 again. But it depends how you've implemented levels and which class the above code is in. A lot more code is needed to be sure of the problem.