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

2018/1/12

changing worlds only when one box is pressed

jacquelinereilly jacquelinereilly

2018/1/12

#
In my game, I have created a menu. It has three boxes on it (which are images I added). When the user presses enter for the box iwith "Items", I want it to go to the world called Menu. I can't figure it out. I have a rectangle that moves when the arrow keys are pressed so it highlights the next box when an arrow key is pressed, and that works fine. It it just the going to Menu only when one is selected. Here is the code:
public class MainMenu extends World
{
    //Variable used to keep track of the cursers location for the update method
    public static int selection = 0;

    //Boolean used to check if user is pressing any of the arrow keys
    boolean press;
    
    /**
     * Constructor for objects of class Menu.
     * 
     */
    public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(25*48, 14*48, 1);
        GreenfootImage Bg = new GreenfootImage ("Background.jpg");

        //Scaling the image
        Bg.scale (getWidth (), getHeight ());
        setBackground (Bg);

        //Adding the title
        Title title = new Title ();
        addObject (title, 610, 122);
      
        //Adding text object for the menu
        addObject (new Text ("Pokemon"), 458, 336);
        addObject (new Text ("Items"), 872, 336);
        addObject (new Text ("Map"), 673, 497);

        //Adding the rectangle image for menu selector
        MenuSelector selector = new MenuSelector ();
        addObject (selector, 458, 336);
    }

    //Need this for when the user presses enter on the Items box. 
    public void act(){

        if (Greenfoot.isKeyDown("enter")) {

            Greenfoot.setWorld(new Menu());

        }

    }

   public void actOne ()
    {
        if (Greenfoot.isKeyDown ("enter") && Trigger.detected ()) {
            WalkingWorld.WorldNumber = 1;
            WalkingWorld.CurrentWorld = 1;
            Greenfoot.setWorld (new InsideArea (50));
        }
        Trigger.update("enter");
    }
     

    /** 
     * This method updates the cursor when certain arrow keys are pressed
     */
    public void update () 
    {
        if (!Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down") && !Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("right"))
        { press = false;} 

        if (Greenfoot.isKeyDown("right") && !press && selection > 1){ //If it's on the first one
            selection += 1;
            press = true;
        } 

        if (Greenfoot.isKeyDown ("down") && !press && selection >1){ //If it's on the first one
            selection -= 2;
            press= true;
        }

        if (Greenfoot.isKeyDown("down") && !press && selection < 2) { //If it's the second one
            selection -= 1;
            press = true;
        }

        if (Greenfoot.isKeyDown ("left") && !press && selection < 2) { //If it's on the second one
            selection -= 1;
            press = true;
        }

        if (Greenfoot.isKeyDown("up") && !press && selection < 3) { //If it's the third one
            selection += 2;
            press = true;
        } 
    }

}
danpost danpost

2018/1/12

#
It might help if you pass the focus-able text objects to the MenuSelector object and put the key control in its class. Then, have the world test a 'selected' field, which is set to the focused text (or its index) when "enterr" is pressed, to proceed to the appropriate world.
You need to login to post a reply.