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

2013/10/26

getWorld().removeObject(object);

sihaco sihaco

2013/10/26

#
Hey people, I have to use the removeObject() method to remove an object of the class Car when it's hit. The class Bullet has a method to detect if it's touching a Car and to remove it when it's true. But, the getWorld().removeObject(Car.class) line throws an error. It says it expects an object as a parameter, but that Car.class is a class, and not an object. But with the canSee() method, I also use Car.class as a parameter to refer to an object of Car, and that works fine. Why can't I give the method getWorld.removeObject() method a similar parameter? getWorld().removeObject(this) in the class Car works fine, but instead of this, I have to refer to the class Car because this time the method to remove the car should come from the class Bullet. Who knows the problem?
Gevater_Tod4711 Gevater_Tod4711

2013/10/26

#
The problem is that when you use Car.class you don't know wich car you mean. So you can not be shure to remove the right one. If you want to remove a car object you need to use the object you want to remove not the class. (Except you want to remove every car in the world. Then you can use getWorld().removeObjects(getWorld().getObjects(Car.class));) If you want to remove the car that your object is touching at the moment you can use this code:
Actor actor = getOneIntersectingObject(Car.class);
getWorld().removeObject(actor);
sihaco sihaco

2013/10/26

#
I have my method like this now:
public void removeCarIfHit()
    {
        if (isTouching(Car.class) && isHit == 0){
            //setImage("bullet-gone.fw.png");
            Actor actor = getOneIntersectingObject(Car.class);  
            getWorld().removeObject(actor);
            isHit = 1;
        }
    }
Now the cars don't disappear when they're hit by a bullet. This code is from the class Bullet.
Gevater_Tod4711 Gevater_Tod4711

2013/10/26

#
Could you post the whole code?
danpost danpost

2013/10/26

#
Is this method being called by the act method of the Bullet class?
sihaco sihaco

2013/10/27

#
Yes, it's called by the act method.
danpost danpost

2013/10/27

#
Try it after removing the 'isHit' part and see if it works, then. If not, then it has something to do with the value of 'isHit' (maybe its value is changed before a car is touched). The code to try is this:
public void removeCarIfHit()
{
    if (isTouching(Car.class)) {
        //setImage("bullet-gone.fw.png");
        Actor actor = getOneIntersectingObject(Car.class);  
        getWorld().removeObject(actor);
    }
}
You need to login to post a reply.