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

2017/6/22

Need help creating menu

Kevroa Kevroa

2017/6/22

#
Hi, I've been trying to understand why this doesn't work but have no idea why. (probably something dumb) The home screen menu loads in but when I press enter, nothing happens. It should remove everything but it doesn't.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Menu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Menu extends Actor
{
    public int selPos = 0;
    /**
     * Act - do whatever the Menu wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        loadHome();
        statements();
    }    
    public void loadHome()
    {
        Background Background = new Background();
        getWorld().addObject(Background,800,450);
        Title Title = new Title();
        getWorld().addObject(Title,800,175);
        Start Start = new Start();
        getWorld().addObject(Start,800,400);
        Options Options = new Options();
        getWorld().addObject(Options,800,600);
        Exit Exit = new Exit();
        getWorld().addObject(Exit,800,800);
        Selector Selector = new Selector();
        getWorld().addObject(Selector,800,400);
    }
    public void unloadHome()
    {
        getWorld().removeObjects(getWorld().getObjects(Background.class));
        getWorld().removeObjects(getWorld().getObjects(Title.class));
        getWorld().removeObjects(getWorld().getObjects(Start.class));
        getWorld().removeObjects(getWorld().getObjects(Options.class));
        getWorld().removeObjects(getWorld().getObjects(Exit.class));
        getWorld().removeObjects(getWorld().getObjects(Selector.class));
    }
    private void statements()
    {
        if (selPos == 0)
        {
            if (Greenfoot.isKeyDown("enter"))
            {
                unloadHome();
                //loadGame();
                selPos = -1;
            }
            if (Greenfoot.isKeyDown("down"))
            {
                //move selector
                selPos = 1;
            }
        }
    }
}
Also, if you could tell me how to move the selector to a different location that would be great.
danpost danpost

2017/6/22

#
Each act step, you are creating new objects and placing them over the previously created ones. When you 'unloadHome', this Menu actor remains in the world and will act to create the objects anew. Use the 'addedToWorld' method to create the menu objects so that you only create one instance of each (when the Menu object is added into the world:
public void addedToWorld(World world)
{
    loadHome();
}
and remove line 18 above.
Yehuda Yehuda

2017/6/22

#
You can also remove the Menu actor when you're finished with it. I think it makes more sense for your Menu to be a separate World and then you can switch between the Menu World and game World using the following.
Greenfoot.setWorld(new MyWorld());

//or you can set an already existing instance (which is what I did above, just looks different)

MyWorld newWorld = new MyWorld();
newWorld.setSomething(/*settings before initialized as world*/);
Greenfoot.setWorld(newWorld);
danpost danpost

2017/6/22

#
Kevroa wrote...
if you could tell me how to move the selector to a different location that would be great.
Do you want the selector at a different location initially or do you want to be able to drag it with the mouse while the program is running? If you could explain why you would want to do this, it would help.
You need to login to post a reply.