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

2014/7/7

Showing explosions when all enemies removed

davemib123 davemib123

2014/7/7

#
I have made good progress on my game, however I have a slight problem. How do I get explosions to appear on the screen when all enemy objects are removed from the screen after collecting the "nuke" powerup. Link to my game is here: http://www.greenfoot.org/scenarios/11783 Any suggestions would be appreciated.
danpost danpost

2014/7/7

#
You will have to remove them one at a time while adding explosions at the location of each one as you remove them.
// instead of 
getWorld().removeObjects(getWorld().getObjects(Enemy.class));
// you need to use
for (Object obj : getWorld().getObjects(Enemy.class))
{
    Actor nuked = (Actor) obj;
    getWorld().addObject(new Explosion(2), nuked.getX(), nuked.getY());
    getWorld().removeObject(nuked);
}
If you need different sized explosions dependent on the type of Enemy object exploding, cast 'obj' to 'Enemy' instead of 'Actor' and get its selection value to determine the Explosion selection value
davemib123 davemib123

2014/7/7

#
thanks, i'll give it a go :)
davemib123 davemib123

2014/7/7

#
this is what I have attempted:
for (Object obj : getWorld().getObjects(Enemy.class))  
        {  
            if (obj instanceof Drone) {
                Drone Drone = (Drone)obj;
                getWorld().addObject(new Explosion(2), Drone.getX(), Drone.getY());  
               getWorld().removeObject(Drone);  
            }
        }     
is that on the correct lines?
danpost danpost

2014/7/7

#
That should be sufficient.
You need to login to post a reply.