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

2017/2/23

Pause and resume

YunitaPutri YunitaPutri

2017/2/23

#
I want to pause the run action using the space key on the keyboard, but I don't know the syntax used to stop, and I want to resume the action that had been paused using the enter key on the keyboard? public void pause(){ if (Greenfoot.isKeyDown("Space")) { ??? } } public void resume(){ if (Greenfoot.isKeyDown("Enter")) { ??? } } }
Super_Hippo Super_Hippo

2017/2/23

#
What is "the action"? Do you want to pause and resume the whole scenario?
YunitaPutri YunitaPutri

2017/2/23

#
Sorry, I mean is the actor. "Do you want to pause and resume the whole scenario?" Yes, that's true
Super_Hippo Super_Hippo

2017/2/23

#
This might work:
if (Greenfoot.isKeyDown("space"))
{
    while (!Greenfoot.isKeyDown("enter"))
    {
        delay(1);
    }
}
Not sure if the 'delay(1)' is needed at all.
YunitaPutri YunitaPutri

2017/2/23

#
cannot find symbol - methode delay(int)
davmac davmac

2017/2/23

#
Use "Greenfoot.delay(1)", not just "delay(1)".
Not sure if the 'delay(1)' is needed at all.
Not technically required, but it's very good practice; otherwise you have a tight loop spinning and potentially consuming 100% of a processor resource just waiting for a key to be pressed. Also, without the delay, you will prevent Greenfoot actions such as "pause" / "reset" from taking effect.
YunitaPutri YunitaPutri

2017/2/23

#
when I run and then push space and enter its doesn't work please check my syntax.. import greenfoot.*; public class Fish extends Actor { int speed = 2; public void act() { run(); toTheRight(); toTheLeft(); toUp(); toDown(); pause(); resume(); } public void run(){ move(1); } public void toTheRight(){ if (Greenfoot.isKeyDown("Right")) { setLocation(getX() + speed , getY()); } } public void toTheLeft(){ if (Greenfoot.isKeyDown("Left")) { setLocation(getX() - speed , getY()); } } public void toUp(){ if (Greenfoot.isKeyDown("Up")) { setLocation(getX() , getY() - speed); } } public void toDown(){ if (Greenfoot.isKeyDown("Down")) { setLocation(getX() , getY() + speed); } } public void pause(){ if (Greenfoot.isKeyDown("Space")) { Greenfoot.delay(1); } } public void resume(){ if (Greenfoot.isKeyDown("Enter")) { Greenfoot.start(); } } }
Super_Hippo Super_Hippo

2017/2/23

#
You did not do what was suggested. Right now, your scenario is only paused as long as the space bar is hold down and the enter key does not have any effect.
davmac wrote...
Not technically required, but it's very good practice; otherwise you have a tight loop spinning and potentially consuming 100% of a processor resource just waiting for a key to be pressed. Also, without the delay, you will prevent Greenfoot actions such as "pause" / "reset" from taking effect.
Interesting, thank you for clarifying that!
YunitaPutri YunitaPutri

2017/2/23

#
Thank you, its work.. I have a lot of knowledge.. Sorry I just a beginner
YunitaPutri YunitaPutri

2017/2/23

#
Like this public void pauseAndResume(){ if (Greenfoot.isKeyDown("space")) { while (!Greenfoot.isKeyDown("enter")) { Greenfoot.delay(1); } } } }
danpost danpost

2017/2/23

#
Because 'pauseAndResume' is called separately from your other method calls in the 'act' method, all it is essentially doing is consuming time during each act cycle. It is not changing what is being done; but, just slightly slowing down the scenario speed when the "space" key is pressed. To prevent the actor from doing anything when the key is down, the other method calls must be within the 'isKeyDown("space")' 'if' block. 'Greenfoot.start();' will not ever be executed (at least, it will never start the scenario) because if the scenario is in a stopped state, the program is not running (no lines in the program are being executed). So, the check for 'isKeyDown("enter")' is pointless.
Super_Hippo Super_Hippo

2017/2/23

#
@danpost: I think the 'pauseAndResume' method replaces and 'pause' and 'resume' methods. This way it should stop the scenario from doing anything when space is pressed until enter is pressed. And if I understood Yunita correctly, it does work now.
danpost danpost

2017/2/23

#
Super_Hippo wrote...
@danpost: I think the 'pauseAndResume' method replaces and 'pause' and 'resume' methods. This way it should stop the scenario from doing anything when space is pressed until enter is pressed. And if I understood Yunita correctly, it does work now.
The problem is, if the scenario is paused, then there will not be any checking on the state of the "enter" key. YunitaPutri has (had) a 'Greenfoot.start();' statement in the 'resume' method which indicates to me that it was in the presumption that the scenario would be in a stopped state. @YunitaPutri, the state of the scenario to be actively paused is something that is more general than what a World object or any Actor object should handle. Its state should be tracked in a more general way. Yes, the state will need to be tracked; but not by any Actor object and not by the World object. The field to track the "paused" state of the scenario would be best placed in your World subclass -- not held by your World object; but, by the class itself:
public static boolean paused;
The object that should detect changes in the state would be the active world. So, your World subclass act method should keep the value of the field up to date:
if (Greenfoot.isKeyDown("enter")) paused = false;
if (Greenfoot.isKeyDown("space")) paused = true;
These should be the first two lines in the method. Then immediately after those line and at the beginning of every Actor subclass act method, place the following line:
if (MyWorld.paused) return;
Replace 'MyWorld' with the name of your World subclass.
danpost danpost

2017/2/23

#
Super_Hippo wrote...
@danpost: I think the 'pauseAndResume' method replaces and 'pause' and 'resume' methods. This way it should stop the scenario from doing anything when space is pressed until enter is pressed. And if I understood Yunita correctly, it does work now.
Ahh. I missed the 'while' bit. Yes -- that would work; except for the fact that you will not be able to actually Pause the scenario (by way of the greenfoot UI 'Pause' button) when the scenario is actively paused.
You need to login to post a reply.