Multiple levels in Greenfoot games

A demo scenario

Yesterday I announced that a new Greenfoot version is available (version 2.1). Over the next few days, I’ll go through some of the new features and introduce them briefly here. This is the first one: the setWorld(…) method.

The Greenfoot class now has a method with the following signature:

setWorld (World world)

This method takes a world object as a parameter, and it will show that world in the Greenfoot main window.

This allows you to have multiple World subclasses (in my example called Level1 and Level2). You can then dynamically (that is: while your program is running) create and install other world objects. These other worlds can show entirely different scenes, such as other levels.

Two world subclasses, representing different levels.

For example, to go to Level 2, you could use the following statement:

   Greenfoot.setWorld(new Level2());

Of course, the same initialisation applies as usual: If the Level2 world creates and inserts some actors in it’s constructor, this will all happen.

One thing to keep in mind, though, is that this will – if you don’t do anything clever – create new actors. So, if you have a game character in level 1, an then you add a game character to level 2, you will have a new character object. If you stored some state (e.g. a score) in the instance variables of that actor, it will be gone. If you want to preserve the state of your game character, make sure you carry the actor object across without creating a new one.

An example showing this new functionality in action is now on the Greenfoot Gallery, here.

Greenfoot 2.1 released

We have just released Greenfoot version 2.1.0.

This update of Greenfoot includes a good number of bug fixes, so if there is something that has bugged you for a while, give the new version a go and see whether it’s improved. (If not, tell us!)

But, more importantly, this version also includes some new functionality. The new features include:

  • A new sound API for volume control. This means Greenfoot scenarios can programmatically adjust the volume of sound clips. Several people have asked for this, especially aiming at fading sounds in and out nicely.
  • A new input function: getMicLevel. This function returns the noise level from the system microphone. This makes it very easy to write soem very simple early interactive examples.
  • Support for multiple worlds. Greenfoot now has a function to dynamically show different world objects. This makes it much easier to develop games with different levels.
  • Built-in move and turn methods. Actors in Greenfoot now have turn(int) and move(int) methods which turn and move relative to the current rotation.
  • UI changes. Finding out how to share your scenario on the Greenfoot Gallery has been made much easier. We suspect that many users previously never discovered this important function.

So, surf over to the Greenfoot download page and give it a go. Let us know what you think.

Over the next few days, I will add a few blog posts here that introduce and explain the new functionality in more detail. So check back soon!

Part 1: Using setWorld to create multiple game levels.