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

2012/4/7

Multiple worlds?

ttamasu ttamasu

2012/4/7

#
I have heard you can have multiple worlds in a senerio. How is the actors connected when you have more than one world. For example in an actor class, if you use getWorld().addObject() ... which world does it use and is there a good example of how it can be used?
danpost danpost

2012/4/7

#
Simply put, 'getWorld()' will return the world that the actor is currently in. Therefore, the object will be added to the same world the actor is in. Example: someone shoots at someone else? a bullet is added to the world with
getWorld().addObject(new Bullet(this), getX(), getY());
I passed 'this' (which is a reference to this actor) to the bullet, so the direction of travel can be determined in the bullet constructor.
ttamasu ttamasu

2012/4/7

#
I know that ... I found what I was looking for.. Its Greenfoot.SetWorld(World). It associates another world the with same actors. I can see how this can create better levels but I was hoping there was a way to keep two independent sets of worlds so I can do some background processing while the current one is being played.
danpost danpost

2012/4/7

#
I am sure you could do this (let us say your sub-class of World is named 'Arena')
Arena arena = new Arena();
// process stuff in 'arena'
// then when needed
Greenfoot.setWorld(arena);
davmac davmac

2012/4/7

#
Its Greenfoot.SetWorld(World). It associates another world the with same actors.
This might just be a matter of terminology, but, no, Greenfoot.setWorld(...) doesn't associate another world with the same actors - it just makes another world active. The other world might have a completely different set of actors in it (or none at all).
I was hoping there was a way to keep two independent sets of worlds so I can do some background processing while the current one is being played.
You can do this by manually calling the act() method of each actor in the other world. Use getObjects(...) to get a list of all the actors, then iterate through it and call act() on each one:
        otherWorld.act();
        List<Actor> l = otherWorld.getObjects(null);
        for (Actor a : l) {
            a.act();
        }
You could do this from in your main world's act() method.
You need to login to post a reply.