I'm looking to build a container to hold multiple Actors. Has this been done before?
An example would be to have an adventure hero with a health bar.
Danpost - I fixed an error in your XWorld class - how do I get it to you?
public List<Actor> getInstances(Class... classes)
{
if (classes.length == 0) return (List<Actor>) getObjects( null ); // error
if (classes.length == 0) return (List<Actor>) getObjects( (Class<Actor>) null ); / corrected
List<Actor> actors = new ArrayList<Actor>();
for (int i=0; i<classes.length; i++) actors.addAll(getObjects(classes[i]));
return actors;
}
import greenfoot.*;
public class Player extends Actor
{
private Healthbar hb = new Healthbar();
protected void addedToWorld(World world)
{
world.addObject(hb, 0, 0);
hb.act();
}
private class Healthbar extends Bar
{
pubic Healthbar()
{
super(< parameters >); // add parameters to create a Bar object for player
}
public void act()
{
if (Player.this.getWorld() == null) getWorld().removeObject(this);
else setLocation(Player.this.getX(), Player.this.getY()-30); // adjust position as needed
}
}
}