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

2013/6/22

How do I set the World the actor exists in again?

bernhard bernhard

2013/6/22

#
I am working on a game similar to Gravity Guy and I want to do the following: When the guy dies I want the level to restart. To do that I tried:
Greenfoot.setWorld(new getWorld());
but that didn't work, gave me an error: "cannot find symbol - class getWorld" so I tried:
World w = getWorld();
Greenfoot.setWorld(new w());
but that gave me the same error. How can I do it?
Gevater_Tod4711 Gevater_Tod4711

2013/6/22

#
You could try to use Greenfoot.setWorld(new getWorld().getClass()); but I'm not shure if this will work. I didn't try. If it doesn't work you will have to check what world you are in like this:
if (getWorld() instanceof Level1) {
    Greenfoot.setWorld(new Level1);
}
else if (getWorld() instanceof Level2) {
    Greenfoot.setWorld(new Level2);
}
//...
You have to use the names of your level classes instead of Level1, Level2, ... (except that are the right names).
I would personally put it in the world itself, because you know that world is going to be able to respawn. Just have a method for all you worlds (hmmm, similar method, maybe they should have a superclass?) that has the name "restart()" or "respawn()". Then you can have your actor call it when he dies.
bernhard bernhard

2013/6/22

#
@Gevatter Tod: your first solution did not work, anyway your second did, it works now. thank you. @FlyingRabidUnicornPig: your solution did not really work but I guess that was because I did not do it correctly, my knowledge about programming is quite small. Anyway thank you for your answer too. Thisis such a great community here on greenfoot.org :)
You need to login to post a reply.