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

2022/7/23

Is it possible that a scenario stops and restarts itself?

lehrerfreund lehrerfreund

2022/7/23

#
I want to perform something like
Greenfoot.stop();
this.turn(111);
Greefoot.start();
I know this idea is somehow bizarre, but perhaps there is an solution which might help me? Thanks in advance!!!
Super_Hippo Super_Hippo

2022/7/23

#
If you use Greenfoot.stop(), the only way to resume is to click the Run button manually. What exactly are you trying to achieve? Is Greenfoot.delay() doing the job? If you need some pause which can manually be resumed with a keypress or something, you could save the pause state in a variable and exit the act method of everything if it is set – except the check to unpause at one place of course.
lehrerfreund lehrerfreund

2022/7/24

#
Super_Hippo wrote...
What exactly are you trying to achieve?
Good question :-) I am using the scenario in school for practicing loops and alternatives etc. Because loops in a running Greenfoot-scenario lead to problems I decided to use stopped scenarios only and trigger the execution by clicking the Act-button. This means that there is no state where the scenario is running. No problem so far. But if I want to use a button e.g. to switch sound on and off or to call a tutorial-text or something I need the scenario running. I am totally aware that the imagination a scenario could start and stop itself is weird, but I give it a try here …
RcCookie RcCookie

2022/7/24

#
I can’t think of a reason why your code should not work. But Is that really what you need? Don’t you want the opposite; a scenario that starts itself on the click of a button and then stops again?
lehrerfreund lehrerfreund

2022/7/24

#
You are right, RcCookie - surprisingly (and understandably) the code is running, because the act-cycle is executed to the very end before the stop-command is executed. So the important thing is to restart the scenario before the method is finished.
RcCookie RcCookie

2022/7/24

#
Actually, the stop() method gets executed immediately. However, it does not terminate the whole program. It just sets some flag to indicate that the act cycle should be paused. If we later call start() again, it does the opposite thing, setting the flag to indicate that the act cycle should continue, and because the flag is only checked once all act methods have been called, nothing will visibly happen.
rdFx rdFx

2022/7/26

#
I'm not sure but this might help you. You can always define methods in the World class that are callable via right click on the scenario screen. So in your case you might want to add a method "showTutorialText()" or "switchSound()" that is defined in your World class (needs to be public i guess) and is callable via right click (a drop down with all methods should be shown). This only works if you don't need to run your scenario as an app but it will work if the scenario is paused in greenfoot!
You need to login to post a reply.