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

2012/1/16

How to make a pause option

Akuhano Akuhano

2012/1/16

#
I looked around the discussions, and I couldn't really find anything helpful for this topic. I want to make my game able to be paused by pressing a key, like "p". However, I don't know the code to do something like this. Can anyone help with this problem? Thanks so much!
Akuhano Akuhano

2012/1/16

#
Never mind about this now, I got a pause option to work.
Duta Duta

2012/1/18

#
I might as well post here anyway, in case someone else has the same problem and wants an answer: There's a couple of ways of doing this, but I'll just post one so its a clearer solution. Oh and a note before I start - any reference I make to YourWorldName should be changed to the name of your world class. - Change your world class to the following:
import greenfoot.*;
//Any other import statements.

public class YourWorldName extends World
{
    private boolean readPause = true;
    public boolean isPaused = false;
    //Any other fields.

    public YourWorldName()
    {    
        //Initialization code.
    }
    
    public void act()
    {
        if(!readPause && !Greenfoot.isKeyDown("p"))
            readPause = true;
        if(Greenfoot.isKeyDown("p") && readPause)
        {
            if(isPaused) isPaused = false;
            else         isPaused = true;
            readPause = false;
        }
        if(!isPaused)
        {
            //Original contents of your act() method (if any).
        }
    }
    
    //Any other methods.
}
- Modify all your actor's act() methods so that they are like the following:
public void act() 
{
    YourWorldName theWorld = (YourWorldName) getWorld();
    if(!theWorld.isPaused)
    {
        //Original contents of your act() method (if any).
    }
}
Andriyanto Andriyanto

2012/7/6

#
i have class player and i have class book, i want if player take a book then game paused, if i press any key game run..can you help me, how i make game pause like that?
Andriyanto Andriyanto

2012/7/6

#
i have class player and i have class book, i want if player take a book then game paused, if i press any key game run again..can you help me, how i make game pause like that?
You need to login to post a reply.