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

2012/10/25

reconstitute the world

HamburgerSV HamburgerSV

2012/10/25

#
I am programing a game with several levels. And if the next level is reached, the world should be the same as at the beginning of level 1. But the variables should not be changed. Can someone help me? Thanks a lot
danpost danpost

2012/10/25

#
If you are changing levels by using 'setWorld' to the same world, you can declare those variables as 'static' so there values are maintained through the world change. Add an instance boolean to the class (call it 'hasStarted') and add the following method to the world
public void started()
{
    if (!hasStarted)
    {
        // reset all 'static' variables}
    }
}
// first statement in world act method
hasStarted = true;
What this does is: (1) if you pause and run the scenario without reseting or re-compiling, 'hasStarted' will maintain its 'true' state and the variables will not reset; (2) if you reset or re-compile, 'hasStarted' will be set back to 'false' and the variables will be reset; (3) if you 'setWorld', 'hasStarted' will be reset to 'false', but the method will not be called to reset the variables and the act() method will return its value back to 'true'. As an alternative, you could just remove all the objects from the world, and re-create the world programatically for each level (instead of changing worlds). When doing so, you need to add the same main actor(s) that you remove. That is, if they contain any information you do not want to loose. By 'main actor(s)', that would include player(s), score counter(s), and the like. Hope this helped.
HamburgerSV HamburgerSV

2012/10/25

#
thank you very much :)
You need to login to post a reply.