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

2013/12/24

World(s) Constructor(s)

Abomb1126 Abomb1126

2013/12/24

#
What calls the World Constuctor when when the game starts in greenfoot? I want to add two worlds side by side, one a "Store" and one the "BalloonWorld", but how do I construct the "Store" world, with the "BalloonWorld" at the same time?
danpost danpost

2013/12/24

#
Only one of the world will be constructed when you compile or reset your project. You can make one load first by default by manually creating a world of that type (right clicking on the world icon and selecting 'new WorldName()'. You can, from any world, create an instance of a different world, or even of the same world, by coding 'new WorldName()'. Once that is done, you may or may not set that new world active:
World w = new Store();
Greenfoot.setWorld(w);
You could create the store while constructing your BalloonWorld and save it in a field to use and re-use later.
// instance field in BalloonWorld class
Store store;
// in the BalloonWorld constructor
store = new Store(this);
// instance field in the Store class
World world;
// begin the Store constructor with
public Store(World w)
{
    // super constructor call -- 'super(int, int, int);'
    world = w;
    // rest of constructor
}
When exiting the store use 'Greenfoot.setWorld(world);'..
You need to login to post a reply.