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

2014/1/29

How to return to a previous world.

swendleys swendleys

2014/1/29

#
Hello, I have a world called "SpaceWorld" and a world called "SpaceShip". If i am in the space and i touch a spaceship then i enter into a spaceship. my question is: How can u return to the same SpaceWorld if i exit the world by touching a object. I don't want to make a new SpaceWorld like
Greenfoot.setWorld(new SpaceWorld());
my goal is actually to change something in the SpaceWorld when i return to the SpaceWorld from my SpaceShip. Thanks.
davmac davmac

2014/1/29

#
You must save a reference to the original world (the SpaceWorld) in your SpaceShip (for example), and then set that world using Greenfoot.setWorld(...) in the usual way. Eg:
// from SpaceWorld:
Greenfoot.setWorld(new SpaceShip(this));

// in SpaceShip:

private SpaceWorld spaceWorld;

public SpaceShip(SpaceWorld spaceWorld)
{
    this.spaceWorld = spaceWorld;
}

public void returnToSpace()
{
    Greenfoot.setWorld(spaceWorld);
}
swendleys swendleys

2014/1/29

#
Thankyou for your reply. but it dind't worked. I have an actor called "SpaceMan". the spaceman must return to the previous world by touching an other actor called "door" in the world "SpaceShip". Thanks
danpost danpost

2014/1/29

#
What davmac suggested should work. If it does not, then you probably need to post the code you used for us to help you with. If you are getting error messages, you should post them also (in their entirety) specifying which line of code is being highlighted (if any).
swendleys swendleys

2014/1/29

#
thank you it worked!!!
swendleys swendleys

2014/1/29

#
but now i have another problem i would like to take the points i have from the SpaceShip world to the previous world this is what i have so far
    public void touchDoor()
    {
        Actor door = getOneIntersectingObject(Door.class);
        if(door != null && getWorld() instanceof SpaceShip)
        {
            SpaceShip t1 = new SpaceShip(World world);
            CounterSwen counter = (CounterSwen)getWorld().getObjects(CounterSwen.class).get(0);  
            CounterSwen t1counter = (CounterSwen)t1.getObjects(CounterSwen.class).get(0);  
            t1counter.setValue(counter.getValue());
            Greenfoot.setWorld(SpaceWorld.mainWorld); 
        }
    }
danpost danpost

2014/1/29

#
Line 6 should not be instantiating another SpaceShip world (you are already in one of those). The method should be using, in lines 8 and 10, the SpaceWorld field where you are holding the first world; so, remove line 6. Well, I do not know, but line 10 looks like it might be doing that (or at least trying to).
You need to login to post a reply.