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

2014/5/20

Removing objects in a certain area...

GP2 GP2

2014/5/20

#
Hello again Greenfoot discussion form, I am currently working on a game that involves a character that can drop bombs where he stands. I want the bombs to wait for a brief period of time and then explode showing an explosion image and removing all actors in the area it covers. My first instinct was using the canSee method but that only covers one object and I want to remove all of them. Here is the code for the bomb 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
public class Bomb extends Actor
{
    private int chargeTime;
    private int delay;
    private GreenfootImage egg;
    private GreenfootImage explosion;
    public Bomb(){
        chargeTime = 10;
        delay = 0;
        egg= new GreenfootImage("eggBomb.PNG");
        explosion = new GreenfootImage("Explosion.PNG");
        setImage(egg);
    }
     
    public void act()
    {
        delay++;
        if(delay >= chargeTime)
        {
            setImage(explosion);//code for destroying all actors in the area would go here
        }
    }
     
}
If anyone has any ideas please help
danpost danpost

2014/5/20

#
'getWorld().removeObjects(getObjectsInRange(getImage().getWidth()/2, null));' I figure your range should be a circular one and not extend beyond the width of the explosion image. You will then need to limit the time the explosion image is showing and remove the actor after that time.
GP2 GP2

2014/6/12

#
Thank you so much Danpost but I have another question is there a way to rewrite this code so it only removes certain objects?
danpost danpost

2014/6/12

#
If you want to remove only objects from specified classes, you can write the line multiple times replacing 'null' with 'ClassName.class' -- one line for each different class type.
You need to login to post a reply.