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

2022/1/30

Screen change

tomzz tomzz

2022/1/30

#
I would like to go to another screen, set another world and then make the user do something on that screen, but when I return to the original screen I would like the world to be as it was left as. When I use to setWorld() it sets the world as a new game. How do I make it so that it sets the world to the present?
Spock47 Spock47

2022/1/30

#
It depends on what object you are giving to the setWorld method. If you call
Greenfoot.setWorld(new MyWorld());
it means that you create a new object of class MyWorld and set this as the current world. Therefore, it is a reset. But here, you don't want to create a new MyWorld object, but reuse the original one. Thus, if instead you call the setWorld method with the original MyWorld object as parameter, it will return to the previous world in the same state as you left it. Therefore, your "another screen" has to remember to which world it should return:
class AnotherScreen {

    private final World returnWorld;

    public AnotherScreen(final World returnWorld) {
        this.returnWorld = returnWorld;
    }

    ...
    public void returnToPreviousWorld() {
        Greenfoot.setWorld(returnWorld);
    }
    ...

}

class MyWorld {

    ...
    public void temporallyGoToOtherScreen() {
        Greenfoot.setWorld(new AnotherScreen(this));
    }
    ...

}
Live long and prosper, Spock47
tomzz tomzz

2022/1/30

#
thank you!!!!
tomzz tomzz

2022/1/30

#
But do you know a way I can add a world on a world? Like a little screen on my current World?
danpost danpost

2022/1/30

#
tomzz wrote...
But do you know a way I can add a world on a world? Like a little screen on my current World?
Two ways to do that (at least). One is to create a world whose background mimics the real world, like in a paused state. It is not a world in a world, but it could be made to appear as such. Another way, which does not involve another world altogether, is to just use an actor for the "little screen". Just have all operations suspended while that actor is in the world. For further details, just ask.
You need to login to post a reply.