Change your condition to this:
(playerRectangle.Intersects(enemyRectangle) && enemyAlive)
and that should do the trick for you.
EDIT: as Zhafur pointed out, there is a simple optimization that can be done:
(enemyAlive && playerRectangle.Intersects(enemyRectangle))
I would point out that the Intersects
method must check all 4 sides of the player's rectangle with all 4 sides of the enemy's rectangle. This would then make this simple call something like 16 smaller checks. If someone knows that figure for certain, please correct me if I am wrong. Personally I would do something like this:
float playerSize = 16f; // These are the _radius_ not the diameter.
float enemySize = 16f;
Then replace the Intersects
method in the condition with a call to Vector2.Distance
:
(enemyAlive && (playerSize + enemySize >= Vector2.Distance(playerLocation, enemyLocation)))
Where playerLocation and enemyLocation represent the center point of the player and enemy.