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

2020/4/4

Just cannot figure out how to change worlds

NotGoodAtThis NotGoodAtThis

2020/4/4

#
I don't know what's wrong with me. I've been at this for at least four hours over several days now. I'm building a game and I need to change the world upon Greenfoot.iskeydown("Enter") I've followed all the solutions I have found online but I still can't do it.

This is the code for the world that opens upon game load.

public class TitleScreen extends World
{

    /**
     * Constructor for objects of class TitleScreen.
     * 
     */
    public TitleScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 250, 1); 
        prepare();
        FirstChange();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        YeOldeKecleonShop yeOldeKecleonShop = new YeOldeKecleonShop();
        addObject(yeOldeKecleonShop,301,170);
        EnterStart enterStart = new EnterStart();
        addObject(enterStart,116,55);
    }
    private void FirstChange()
    {
        if(Greenfoot.isKeyDown("enter"))
        {
        Greenfoot.setWorld(new StartScreen());
        }
    }
}
Yes. I'm using the right name for the world to move to. This seems like such a beginner thing and I still can't do it. One more question. For when I export my game as a .JAR; how do I choose which world opens immediately upon running? Thanks in advance to whoever can help.
danpost danpost

2020/4/4

#
The FirstChange method needs to be called repeatedly, until the enter key is detected. Right now, it is only being called once during TitleScreen world creation. (you need an act method) A scenario exported as JAR will open with the same initial world as that within the greenfoot application. Within greenfoot, you can choose the initial world simply by manually creating a world of that type ( right click on world class and select, for example, new TitleWorld() ).
NotGoodAtThis NotGoodAtThis

2020/4/4

#
From what I understand, the act method isn't like any other. How do I put "FirstChange();" in the act method so that it keeps running? Solved that. I moved the world change code to an actor that sits on the world I want to move from. Thanks. That's how I expected exporting to work.
danpost danpost

2020/4/4

#
NotGoodAtThis wrote...
Solved that. I moved the world change code to an actor that sits on the world I want to move from.
There is an act method defined in the greenfoot.World class. You can override it (just like it is done in a class extending greenfoot.Actor) in your TitleScreen class. It is just not provided by the World subclass template as it is when you create a new subclass of Actor.
You need to login to post a reply.