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

2019/11/16

Switching between Worlds

ihjufaeshiju ihjufaeshiju

2019/11/16

#
Im trying to switch from SpaceWorld to ShopWorld. I've read that having a menu in another world would essentially pause the original world, but this is not the case for me. I am switching worlds like this:
Greenfoot.setWorld(new ShopWorld()); //go to ShopWorld
Greenfoot.setWorld(new SpaceWorld()); //go back to original world
I think it generates a new world instad of returning to the old world. How can i change this? I've tried it without the "new" and without the "()" but that did not work.
danpost danpost

2019/11/16

#
Your second line does not go back to the original world. It goes to a new instance of SpaceWorld. In order to go back to the same SpaceWorld object, the ShopWorld object would need to have a reference to that world:
// in ShopWorld
private World spaceWorld;

public ShopWorld(World world)
{
    super(600, 400, 1);
    spaceWorld = world;
    // etc.
}

public void act()
{
    if ( ??? ) Greenfoot.setWorld(spaceWorld);
    // etc.
}

// in act of SpaceWorld (or from actor in the world)
if ( ??? ) Greenfoot.setWorld(new ShopWorld(this)); // 'this' or 'getWorld()'
ihjufaeshiju ihjufaeshiju

2019/11/16

#
Thanks, this worked very nicely.
You need to login to post a reply.