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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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()); } } } |