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

2012/8/25

Switching Worlds In Greenfoot

al_griff al_griff

2012/8/25

#
So I'm making my own version of pacman, and by now I have a fully functional game, but I only have the game screen. So now I need to make a start screen, and lossScreen and a winScreen, but I've got no idea on how to make this work. Would anyone be able to help me? If you need any more additional info, I'd be happy to share the code. Thanks
SPower SPower

2012/8/25

#
If you made those screen out of different World subclasses (like he title says), you can you Greenfoot.setWorld(world). Example:
// let's say the lose screen world is called LoseWorld:
LoseWorld w = new LoseWorld();
Greenfoot.setWorld(w);
Gevater_Tod4711 Gevater_Tod4711

2012/8/25

#
If you want to change the world there is a method in Greenfoot: Greenfoot.setWorld(new nameOfYourWorld()); But I think you could also add an object for a menu or a win or loose screen. This would easier I think. Also see this page
al_griff al_griff

2012/8/25

#
so would I do something like
 public void endGame()
    {
        if (sPCounter > 155 )
        {
           Greenfoot.setWorld(newWorld());
    }
where newWorld was a constructor in the world class with all my settings for the world? or should I do it like
public void endGame()
    {
        if (sPCounter > 155 )
        {
           newWorld newWorld = new newWorld();
Greenfoot.setWorld(newWorld);
    }
with newWorld still being a constructor in the world class. Also that sPCounter is a public value in my pacman class.
danpost danpost

2012/8/25

#
The first code-set would be calling a method, if there was one, not a constructor (with 'newWorld()'). The second code-set is a correct way to instantiate a new world. You could also combine lines 5 and 6 with:
Greenfoot.setWorld(new newWorld());
However, if your game over worlds ('WinWorld' and 'LoseWorld') are to display the final score, you will either have to pass the score itself, or the object that holds the score, in a parameter so you can access it. If you are wanting to do this, and are unsure how, check out this discussion.
You need to login to post a reply.