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:
PauseMenu
Resume button
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(); } }
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); } }
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()); } } }