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

2012/1/31

how do i stop simulations?

craigv craigv

2012/1/31

#
I've been using Greenfoot.stopSimulation(); says not a statement thx for any help again :)
danpost danpost

2012/1/31

#
Try
Greenfoot.stop();
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.
danpost danpost

2012/2/1

#
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).
craigv craigv

2012/2/1

#
how do i set up return statements and where are they placed in my code
danpost danpost

2012/2/2

#
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.
You need to login to post a reply.