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

2012/4/3

Switching between worlds.

jam0037 jam0037

2012/4/3

#
I'm trying to figure out how to have switching between worlds. I have created several new subclasses of the world. I want to know if there's a way of switching between the worlds when the actor is at a certain edge of the world. (eg. say the player is at the top edge of world1 so he goes to world2. but if he's at the right edge, then he goes to say, world3) can anybody help me with this. I've searched through my greenfoot book, but it doesn't even tell me how to switch between two worlds.
danpost danpost

2012/4/4

#
The book would not have anything about it, as the functionality of switching worlds is fairly new. If you are in the class of the actor that reaches an edge of the world (that is (a) moving left and getX() = 0; or (b) moving up and getY() = 0; or (c) moving right and getX() = getWorld().getWidth() - 1; or (d) moving down and getY() = getWorld().getHeight() - 1), and the world you want to go to is called 'UpWorld' (for case b), then use 'Greenfoot.setWorld(new UpWorld());'.
jam0037 jam0037

2012/4/4

#
Thank you for answering! What if I have more than five worlds? is there a way to be able to move from world to world in a specific order? Say I have four or five worlds, and I want the actor to go to the top edge for each one to go to the next one. Is there a way to have such a setup?
danpost danpost

2012/4/4

#
Yes, you can check to see what world is active with
if (getWorld() instanceof (World2))
jam0037 jam0037

2012/4/5

#
Thank you so much!
davmac davmac

2012/4/5

#
There are some extra parentheses in danpost's suggestion that you need to remove for it to compile:
    if (getWorld() instanceof World2)  
You need to login to post a reply.