danpost wrote...
If you use that code, ver batim, you would need a 'Powerup' class.import greenfoot.*; public class Powerup extends Actor { static final int SPEED_PU = 0, DRAG_PU = 1, AMMO_PU = 2; // add as needed static final GreenfootImage[] images = { "speedPU.png", "dragPU.png", "ammoPU.png" }; // add as needed; keep the same order as the static final int fields above int puType; // a value from the list of static final ints above int lifespan = 300; // about 5 to 6 seconds // a Powerup object can be created with 'new Powerup(Powerup.SPEED_PU)' or similar public Powerup(int type) { puType = type; setImage(images[powerup]); } public void act() { lifespan--; if (lifespan == 0) getWorld().removeObject(this); } public int getType() { return puType; } }
/** instead of */ Actor actor = getOneObjectAtOffset(0, 0, Powerup.class); if (actor != null) { getWorld().removeObject(actor); // etc. /** you would use */ Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class); if (pu != null) int kind = pu.getType(); getWorld().removeObject(pu); if (kind == Powerup.SPEED_PU) { /** whatever */ } if (kind == Powerup.DRAG_PU) { /* whatever */ } // etc. }
import greenfoot.*; public class Powerup extends Actor { static final int SPEED_PU = 0, DRAG_PU = 1, AMMO_PU = 2; // add as needed static final GreenfootImage[] images = { "speedPU.png", "dragPU.png", "ammoPU.png" }; // add as needed; keep the same order as the static final int fields above int puType; // a value from the list of static final ints above int lifespan = 300; // about 5 to 6 seconds // a Powerup object can be created with 'new Powerup(Powerup.SPEED_PU)' or similar public Powerup(int type) { puType = type; setImage(images[powerup]); } public void act() { lifespan--; if (lifespan == 0) getWorld().removeObject(this); } public int getType() { return puType; } }
/** instead of */ Actor actor = getOneObjectAtOffset(0, 0, Powerup.class); if (actor != null) { getWorld().removeObject(actor); // etc. /** you would use */ Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class); if (pu != null) int kind = pu.getType(); getWorld().removeObject(pu); if (kind == Powerup.SPEED_PU) { /** whatever */ } if (kind == Powerup.DRAG_PU) { /* whatever */ } // etc. }