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

2012/1/30

how do I remove objects

craigv craigv

2012/1/30

#
on, collision, i want one of two objects to be removed. I dont know where to properly start
danpost danpost

2012/1/30

#
You can check for collision in either of the two object classes. Normally, though, it is done in the object that will be remaining in the world (and it makes sense to do it here). Of course, if it is a random decision as to which one stays, it really does not matter. The reason it is normally done in the one that stays is that oftentimes the collision is recorded somehow (either a counter or a scoreboard), and once you remove the one object, it no longer has any reference to the world. As far as coding, use the getOneIntersectingObject(Class class) method to see if one of those type objects have collided with the instance object in question. Let us say you have a Monster object and a Shot object, then in the Monster class:
Shot shot = getOneIntersectingObject(Shot.class);
if (shot != null) {
    getWorld().removeObject(shot);
    timesShot++;
    if (timesShot == maxShotCount) getWorld().removeObject(this);
}
(maxShotCount would be an instance integer variable for the Monster class that was initialized to a number representing the number of shots required to eliminate the monster).
craigv craigv

2012/1/31

#
when writing this code i get the error message: incompatible types - found greenfoot.Actor but expected Shot
craigv craigv

2012/1/31

#
never mind i fixed my problem :)
You need to login to post a reply.