danpost wrote...
If you use that code, ver batim, you would need a 'Powerup' class.

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 27 28 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** 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. } |
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 27 28 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** 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. } |