Actually, I just now was in consideration of this statement that I provided here. I find execution of the current method continues even after the statement given is executed. So, it might be wise to have it the last statement in the method, or create and call a new method, so it is the last statement in the method.
OK, I tried what I said in my last post (creating and calling a new method, etc). It still passed up the 'stop' statement, returned back to the act() method and continued execution. So, I put a 'return;' statement after the call and now (because the current act cycle of that object has ended) Greenfoot stopped execution of any code and my Pause button became a Run button.
To sum up, the 'stop' statement must be the last statement, or a 'return' statement must follow it (or further statements will execute).
A 'return' statement is just that. It is a branching statement the does an immediate exit from the current method.
So, let us say that if 'timeToStop()' is a method that returns a true/false value by doing a check to see if the conditions are met to stop the simulation, then in an act() method you could have (in either the world or an actors):
// some coding here
if (timeToStop())
{
Greenfoot.stop();
return;
}
// some more coding here
The return statement prevents any more statements in the method from executing, which in turn allows the system to register the 'stop()' command.