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

2017/12/25

Static variable not resetting

Diactaeon Diactaeon

2017/12/25

#
I'm creating a game where aliens lose if their population becomes 0. I use a static variable to do this. The game starts off with 2 aliens, and the static variable is set to 0. Therefore, when the static variable = -2, there should be no aliens, and I want the game to stop at this point and reset the static variable to 0. However, the game neither stops nor sets the static variable to zero. I have provided the code below for stopping the game: public void checkGameOver2() { if (alienCounter == -2) { //the static variable is the alien counter Greenfoot.stop(); getWorld().showText("Aliens lose!", 400, 400); alienCounter = 0; } } Is there something wrong with my code or is there a bug?
xbLank xbLank

2017/12/25

#
The code can never be executed since you call a Greenfoot.stop() first. That should be the last method you call since it pauses your whole Scenario. Also you just showed us the Method itself, not the way you use it in your main act method.
danpost danpost

2017/12/25

#
일리ㅏ스 wrote...
The code can never be executed since you call a Greenfoot.stop() first. That should be the last method you call since it pauses your whole Scenario.
Calling 'Greenfoot.stop()' does not in any way cause a branch in program execution. That is, it will not prevent the rest of the method from executing. Only a 'return' statement or a run-time error can do that. The 'stop' method only sets a flag that will be checked later (between act steps, I believe) to pause the scenario.
Also you just showed us the Method itself, not the way you use it in your main act method.
Chances are you either are not calling the 'checkGameOver2' method from the act method or it is a conditional calling.
xbLank xbLank

2017/12/25

#
danpost wrote...
Calling 'Greenfoot.stop()' does not in any way cause a branch in program execution. That is, it will not prevent the rest of the method from executing. Only a 'return' statement or a run-time error can do that. The 'stop' method only sets a flag that will be checked later (between act steps, I believe) to pause the scenario.
That is correct. I did not know that. But I agree with danpost. Check if you are using it in your act method. I don't think you are.
You need to login to post a reply.