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

2013/11/12

Return to the current world

winsticknova winsticknova

2013/11/12

#
I'm trying to make a game and when you lose it takes you to a game over screen. I want to make it so when you click on the game over screen it returns you to the current world you are in ( there are several different levels in the game that are all separate worlds). How would I code it so I can get the current world and return the actor to the current world?
danpost danpost

2013/11/12

#
Is the game over screen an actor or a world? What is the code for that class?
winsticknova winsticknova

2013/11/12

#
the game over is an actor in the world. and the code for it is
public void click()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(getWorld());
            
        }
    }
but I don't think the getWorld() is returning a new world so it doesn't switch.
danpost danpost

2013/11/12

#
You can remove line 3 and change line 6 to 'Greenfoot.setWorld(new WorldName());' where 'WorldName' is the name of your world. Wait. To return to the current state, use 'getWorld().removeObject(this);'.
winsticknova winsticknova

2013/11/12

#
sorry. I had that at first but the 'Greenfoot.setWorld(new WorldName());' would be restricted to one world. Say I'm in WorldOne and I lose, I want the function to return me to WorldOne, but then if I'm in WorldTwo and I lose, I want the function to return me to WorldTwo. Is there a function to return a new version of the current world that changes as the world changes?
danpost danpost

2013/11/12

#
You would then need to determine what world to reset to:
if (getWorld() instanceof WorldOne) Greenfoot.setWorld(new WorldOne());
else if (getWorld() instanceof WorldTwo) Greenfoot.setWorld(new WorldTwo());
// etc.
winsticknova winsticknova

2013/11/13

#
thank you so much! it works
You need to login to post a reply.