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

2019/2/26

pause and resume with same button?

petyritonel petyritonel

2019/2/26

#
is there a way to pause and resume the scenario with the same button? currently i have to hold escape for it to stay paused.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (Greenfoot.isKeyDown("escape"))
        {
            pause Pause = new pause();
            addObject(Pause,500,250);
            change="space";
            while (!"escape".equals(change))
            {
                change=Greenfoot.getKey();
                Greenfoot.delay(1);
                if(Greenfoot.isKeyDown("z"))
                {Greenfoot.setWorld(new menu());
                }
            }
            removeObject(Pause);
        }
danpost danpost

2019/2/27

#
petyritonel wrote...
is there a way to pause and resume the scenario with the same button? currently i have to hold escape for it to stay paused. << Code Omitted >>
It is not a good idea to have a while loop in which breaking out of it requires an action of the user. Add a boolean field to track the state of the "escape" key. That, along with whether a Pause object is in the world or not should be enough to be able to control everything properly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// using a field
private boolean escapeDown;
 
// in action code
if (escapeDown != Greenfoot.isKeyDown("escape")) // change in key state?
{
    escapeDown = !escapeDown; // new state tracked
    if (escapeDown) // new state is down?
    {
        if (getObjects(Pause.class).isEmpty()) // pausing?
        {
            addObject(new Pause(), 500, 250);
        }
        else // resuming
        {
            removeObjects(getObjects(Pause.class));
        }
    }
}
The check for "z" (going to menu) can be done separately. All actors will probably need to check for the Pause object in the world before acting.
petyritonel petyritonel

2019/3/3

#
thank you
You need to login to post a reply.