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?


1 2 3 4 5 6 7 8 | 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 } } } |