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

2019/8/4

Making a pause menu help

lyliux lyliux

2019/8/4

#
Hi everyone! I'm trying to make a pause menu for my game. What should happen is when "P" is pressed, the pause menu (which is the PauseMenu world) appear, and then when the resume button is pressed, it should go back to GameWorld, with the timer and character in the same location/number. So far when i press resume, nothing happens. GameWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;

/**
 * Write a description of class GameWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameWorld extends World
{

    static GreenfootSound bgMusic = new GreenfootSound("GameMusic.mp3");
    
    /**
     * Constructor for objects of class GameWorld.
     * 
     */
    public GameWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setBackground(new GreenfootImage("gamebg.png"));
        prepare();
    }
    
    private void prepare()
    {
        int[][] grid = new int[][]{
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };
        
        int x = 80;
        int y = -25;
        
        for(int i=12; i>-1; i--)
        {
            y = y + 32;
            x = 80;
            for(int j=12; j>-1; j--)
            {
                x = x + 32;
                if(grid[j][i]==1)
                {
                   Ground topedge = new Ground("topedge1.png"); 
                   addObject(topedge,x,y);
                }
                else
                {
                    Blockage blockage = new Blockage();
                    addObject(blockage,x,y);
                }
            }
        }
        
        Character character = new Character();
        addObject(character,300,200);
    }
    
    public int time = 4100;
    boolean portal = false;
    //6100
    public void act()
    {
        if (getObjects(Character.class).size() == 0)
        {
            showText("", 50, 50);
            showText("Survived: "+ time/100+"s", 300, 300);
        }
        else if (time > 1)
        {
            countTime();
        }
        else if ((time == 1) && (portal == false))
        {
            portal = true;
            showText("", 50, 50);
            showText("The portal", 60, 50);
            showText("is open!",60,80);
            addObject(new Portal(),300, 200); 
        }
        
        if (Greenfoot.isKeyDown("P"))
        {
            World gameworld = this;
            Greenfoot.playSound("File0102.wav");
            bgMusic.pause();
            Greenfoot.setWorld(new PauseMenu(gameworld));
        }
        
    }
    
    public void addEnemies()
    {
        if (time == 6000)
        {
            Sideways sideways = new Sideways();
            //addObject(sideways, rand.nextInt(), rand.nextInt());
        }
    }
    
    private void countTime()
    {
        time--;
        showTime();
    }
    
    private void showTime()
    {
        showText("Time: " + time/100, 50, 50);
    }
    
    public void started()
    {
        bgMusic.playLoop();
    }
 
    public void stopped()
    {
        bgMusic.stop();
    }
}
PauseMenu
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PauseMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PauseMenu extends World
{

    public World lastWorld;
    
    /**
     * Constructor for objects of class PauseMenu.
     * 
     */
    public PauseMenu(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(300, 200, 1);
        lastWorld = world;
        prepare();
    }

    public World getLastWorld()
    {
        return lastWorld;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Resume resume = new Resume();
        addObject(resume,150,80);
        Menu menu = new Menu();
        addObject(menu,150,120);
    }
}
Resume button

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pause here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Resume extends Button
{
    /**
     * Act - do whatever the Pause wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        spriteSet("resume_button.png","resume_button_transparent.png");
    }   
    
    public void resume()
    {
        World gameworld;
        gameworld = getWorld();
        if (Greenfoot.mouseClicked(this))
        {
            playSound("File0103.wav");
            GameWorld.bgMusic.play();
            
            PauseMenu pausemenu = (PauseMenu)getWorld(); 
            Greenfoot.setWorld(pausemenu.getLastWorld());
        }
    }
}
danpost danpost

2019/8/4

#
Though you have not shown the Button class codes (which may be relevant), the naming of the methods in the Resume class is suspect.
lyliux lyliux

2019/8/4

#
danpost wrote...
Though you have not shown the Button class codes (which may be relevant), the naming of the methods in the Resume class is suspect.
Here is the button class code if that is of any use!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button extends Actor
{
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }
    
    public void playSound(String sound)
    {
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.playSound(sound);
        }
    }
    
    public void spriteSet(String img1,String img2)
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null) {
            //change the file to what you want for when the mouse is not over the button.
            setImage(img1);
            List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Button.class);
            for (Object object : objects)
            {
                if (object == this)
                {
                    //change the file to what you want for when the mouse is over the button.
                    setImage(img2);
                }
            }
        }
    }
}
lyliux lyliux

2019/8/4

#
I just realised I never called the resume() method in act(), easy fix! Thank you! I should of realised that sooner haha :')))))
You need to login to post a reply.