I'm making a shmup using farseer, so I can have some decent physics action going on, and I'm using bodies and geoms for the bullets and enemies.
Problem is, when they collide, as soon as they collide I make the bullets inactive and do enemyHealth-- but no matter what I try, the collision event keeps looping, so all enemies die in one hit regardless of their health, because it loops that many times.
I can set the enemyState to "gettingHit" and then do enemyHealth-- in the update loop, but that means that if the enemy gets hit by more than one bullet simultaneously, it only subtracts 1 health, which isn't any use for my game design.
I've tried subtracting the health in the event in the Enemy class, and from the Bullet class too, but OnCollision just gets called way too many times...
Here's the code I have at the moment..
//-- BULLET CLASS -- //
private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
if (geom2.CollisionGroup == 5) // 5 = Enemy Class
position = new Vector2(1500, 1000); // Set bullet off-screen
bulletBody.Position = new Vector2(position.X + offsetX, position.Y + offsetY);
bulletGeom.CollisionEnabled = false;
currentState = (int)bulletState.dead;
Active = false;
Enemy enemy = (Enemy)geom2.Tag;
//-- ENEMY CLASS --//
internal virtual void gotHit()
if (currentState != (int)enemyState.killed && currentState != (int)enemyState.dead) //Killed = dying animation, Dead = removed
currentState = (int)enemyState.hit;
I've tried adding the bullets that hit to a List and then removing them in the update loop for the enemy, so that it subtracts 1 health for each bullet in the list, but it came up with an error saying that the Collection size had been modified. I didn't
think it was an efficient way to do it anyway..
Just to note, my bullets are part of a List that will get re-used later.
Any ideas how I can make sure that the event only gets called once per bullet collision??