I'm having some issues with collision detection in my game. When my Hero dies I have it respawning in the same position or very close to it. The problem is, oftentimes it repawns right on top of the enemy and immediately dies again. This is especially true with my Boss enemy, which is quite a bit bigger than the average enemies. My hero has 10 lives, but if it gets stuck on the side or in a corner and is hit by an enemy it just keeps dying and respawning until all its lives are gone.
I think the solution might be to define a boolean in my Hero's class :
boolean touchingEnemy = false;
but I'm not 100% sure where to take it from there.
Here is my code:
Any help would be appreciated. Thank you in advance!
private void checkBossCollision() { if (getWorld() == null) return; Actor a = getOneIntersectingObject(Boss.class); if (a != null) { if (touchingEnemy == false) { int x = Greenfoot.getRandomNumber(340); lives--; counter.loseLife(); Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); space.addObject(new Rocket(), 65, x); space.addObject(this, 65, x); movement.setNeutral(); touchingEnemy = true; } } if (lives <= 0){ Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); GameOver gameover = new GameOver(); space.addObject(gameover, space.getWidth()/2, space.getHeight()/2); } else{ touchingEnemy = false; }