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

2021/3/14

Help with the pause button

AnJoMorto AnJoMorto

2021/3/14

#
(I feel so ashamed for needing to ask so many questions here, and not being able to help other people, but once again I need your help) I'm trying to add a pause button to my game. I've coded so when the image used is the "pause button" the game is running, and I would like to make so when the "play icon" is being used all the actors that are subclasses of SmoothMover() stop acting. I thought of doing something like this: Game class (subclass of World):
    /**
     * Will pause the game, stopping all actors from moving and will show Main Menu and Replay option
     */
    public void pauseGame()
    {
    
        addObject(new Replay(), WIDE/4    , HIGH/2);
        addObject(new Replay(), WIDE/4 * 3, HIGH/2);
        
        try
        {
            
            getObjects(SmoothMover.class).notifyAll();
            
        } catch(IllegalMonitorStateException e)
        {
            
            System.out.println("Error with SmoothMover class: " + e.getMessage());
            System.exit(-1);
        
        }
    
    }
    /**
     * Will resume the game, if there was a pause menu before, 
     * Panels will be removed and the actors will be awaken
     */
    public void resumeGame()
    {
        
        removeObjects(getObjects(Panels.class));
        
        try
        {
            
            getObjects(SmoothMover.class).wait();
            
        } catch(InterruptedException e)
        {
            
            System.out.println("Error with SmoothMover class: " + e.getMessage());
            System.exit(-1);
        
        }
    
    }
Pause button (subclass of Actor):
    public void act()
    {
        
        if(this.getImage() == images[0])
        {
            
            ((Game) getWorld()).resumeGame();      //resumes the game
        
        } else {
        
            ((Game) getWorld()).pauseGame();      //pauses the game 
        
        }
}
But it doesn't work (the IllegalMonitorStateException is catch directly when I press run). Can anyone help me achieve this, please?
danpost danpost

2021/3/14

#
You are using thread controls (wait and notifyAll) where non-ownership disallows there use. Here are two ways to accomplish what you are trying to achieve: (1) put all act method codes except for the button in if blocks with the condition that the button is in game play state or (2) go to a different world when pausing and return to game world to resume
AnJoMorto AnJoMorto

2021/3/14

#
danpost wrote...
(2) go to a different world when pausing and return to game world to resume
How do I make this second option without the whole level restarting?
danpost danpost

2021/3/14

#
AnJoMorto wrote...
How do I make this second option without the whole level restarting?
By using this:
Greenfoot.setWorld(new PauseWorld(this));
(or getWorld() instead of this) with this (basically):
import greenfoot.*;

public class PauseWorld extends World
{
    World pausedWorld;
    
    public PauseWorld(World world)
    {
        super(600, 400, 1);
        pausedWorld = world;
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(pausedWorld);
    }
}
danpost danpost

2021/3/14

#
I also have a Pause World Class scenario. The Pause class duplicates the current visual of the paused world so as to appear that the world is still present, but paused.
AnJoMorto AnJoMorto

2021/3/14

#
Once again @danpost your help is so appreciated! This works like wonders, and it ended up way prettier than I would ever even think of
You need to login to post a reply.