I'm currently creating a top down shooter styled game and have started to add colision detection. I'm currently working on when the player shoots the enemy it is removed from the wold, and when the bullet hits the enemy the bullet is also removed from the enemy. I am able to make both of these work seperatly but I'm not able to combine the two to work consistantly together.
I currently have three classes, a Player class, a Bullet class and an Enemy class, which I have not added collision detection to yet.
I used the code below, stored in the enemy class, to detect when the bullet contacts the enemy and to delete when it does:
And this is the code, stored in the bullet class, used to remove the enemy when it is hit with a bullet:
1 2 3 4 5 6 7 8 9 | public void bulletDetection() { Actor a = getOneIntersectingObject(Bullet. class ); if (a != null ) { getWorld().removeObject(a); } } |
1 2 3 4 5 6 7 8 9 | public void hitDetection() { Actor b = getOneIntersectingObject(Enemy. class ); if (b != null ) { getWorld().removeObject(b); } } |