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

2012/10/1

Simple toggle for a 'pause menu' help! :D

Codybean1 Codybean1

2012/10/1

#
Hello good people. I want to make a simple in-game pause menu where I can toggle it on and off by pressing enter. All this code does is bring up the menu. I've been looking for quite some time and can't seem to find some simple toggle logic to achieve this feat. Some help would be wonderful. :) public void act() { if (Greenfoot.isKeyDown("enter")) { setImage("Pause Menu.png"); } } }
erdelf erdelf

2012/10/1

#
a boolean should work.
boolean paused;

 public void act() 
{ 
      if (Greenfoot.isKeyDown("enter")) 
      { 
         if(!paused)
         {
               setImage("Pause Menu.png");
               paused = true;
         } else
        {
               setImage("normal Image");
               paused = false;
        }
      } 
} 
danpost danpost

2012/10/2

#
@erdelf, by using 'isKeyDown(...)' you could end up in either state upon release of the key; better is to use 'getKey()' for this. It is much easier, however, to just set a different world for the menu and return to the original world upon menu exit. By handling the menu in this manner, ALL actions in the original world are automatically paused.
// in the actor class where you check for keystrokes
// first check 'isKeyDown(...)' for movement keys, if any; then
String key = Greenfoot.getKey();
if (key != null && "enter".equals(key)) setWorld(new MenuWorld(getWorld());
// and the MenuWorld class
import greenfoot.*;

public class MenuWorld extends World
{
    MainWorld world = null;

    public MenuWorld(MainWorld main)
    {
        super(600, 400, 1); // adjust to size of your main world
        world = main;
        addObject(new Menu(), 300, 200);
    }
}
// in whatever class you check for keystrokes while the menu is up,
// (if not checking for keystrokes anywhere, put in in MenuWorld act method)
    String key = Greenfoot.getKey();
    if (key != null && "enter".equals(key)) setWorld(world);
// if in an actor class
    String key = Greenfoot.getKey();
    if (key != null && "enter".equals(key)) setWorld(((MenuWorld) getWorld()).world);
erdelf erdelf

2012/10/2

#
I dont think a new world for the menu will pause the game, I think it will delete the current progress and the game would be started from the beginning
danpost danpost

2012/10/2

#
@erdelf, check out my Crayon Babies scenario. If you hold a reference to the world you left and return to that same world using that reference, everything will be exactly as you left it.
Codybean1 Codybean1

2012/10/3

#
boolean paused; public void act() { String key = Greenfoot.getKey(); if (key != null && "enter".equals(key)) { if(!paused) { setImage("Cardboard info.png"); paused = true; } else { setImage("Pause Menu Invis.png"); paused = false; } } This part works fine alone. But when I try something like this for other purposes (in this case, toggling the use of an item), the pause menu thing ceases to function. When I get rid of the other derivatives of the function , the Pause Menu works again. I'm sure something is conflicting but I don't know what. Here's an example of something that breaks the pause menu when implemented: boolean box; String key = Greenfoot.getKey(); if (key != null && "z".equals(key)) { if(!box) { box = true; } else { box = false; } } What is conflicting? I want more than one of these types of functions to coexist! :)
danpost danpost

2012/10/3

#
Do NOT use 'Greenfoot.getKey()' twice in the same act sequence. The second time will most assuredly return 'null'. It is best to do you checks together (sequentially). That is
String key = Greenfoot.getKey();
if (key != null && "enter".equals(key))
{
    // some code
}
// do not call 'Greenfoot.getKey()' again here
if (key != null && "z".equals(key))
{
    // some code
}
You could create a method called 'checkKeys' with the above code and call it from your act method.
Codybean1 Codybean1

2012/10/3

#
I mean that I have two different classes that I want using this type of code. The box example was from a class called 'InDatBox'. Where can I put String key = Greenfoot.getKey(); so more than one class can use it for their respective actions?
danpost danpost

2012/10/3

#
There are at least two ways to accomplish what you need to do. One involves getting and saving the 'key' in the world class and checking the saved value from the classes in question. The other involves using 'isKeyDown' with a boolean (so the action is only done one time) in those classes.
// FIRST WAY
// in world class
// instance variable
String key = null;
// in act method
key = Greenfoot.getKey();
// add method
public boolean isKey(String keystroke)
{
    if (keystroke.equals(key))
    {
        key == null;
        return true;
    }
    return false;
}
// in actor classes
if (((WorldName) getWorld()).isKey("enter"))
{
    // some code
}
// SECOND WAY
// in actor classes
// instance variable
boolean enterDown;
// in act method (or method it calls)
if (!enterDown && Greenfoot.isKeyDown("enter")) enterDown == true;
if (enterDown && !Greenfoot.isKeyDown("enter"))
{
    enterDown == false;
    // some code
}
Actually, in the second way you have your choice of having the action occur when the key is pressed or released.
Codybean1 Codybean1

2012/10/3

#
Thanks for the help! Just one more thing: How do I make an 'AND Gate'? I know OR gates use || inbetween functions, is there an AND equivalent?
danpost danpost

2012/10/3

#
See lines 27 and 28 of my previous post in this discussion. Also, you may want to check out Language Basics in the Java Tutorials.
You need to login to post a reply.