Back again from my last question. Now I was curious I use a foreach statement to use the same shot class. How would I go about doing collison detection.

I used the tutorial here on how to shoot a fireball
XNA Development: Game Development for the masses.

I tried to put in several places a foreach to look at all of them to see if they have reached the borders of my sprite hero but doesn't seem to do anything. If again some one might know of a good site that has tutorials to explain collision detection a little bit better that would be appriecated.


1 Answer 1


The fireball is a 'box' of width and height 200. And it's at a given position. Add a rectangle

Rectangle boundingRect = new Rectangle(position.X, position.Y, 200, 200); 

to your fireball class.

Make sure to update the rectangle with the new position of the fireball every frame (so in the fireball's update or draw method)

Do the same thing for your player class.

Now you can check if the fireball has hit the player like this in your game's update method:

if(fireball.boundingRect.Intersects(player.boundingRect)) {
  • \$\begingroup\$ I have tried that before with no results. Where would I put the rectangle portion and where would I put the if statement. \$\endgroup\$
    – user5476
    Feb 15, 2011 at 20:26
  • \$\begingroup\$ add the rectangle portion to your fireball class (now you fireball has a rectangle, which will follow the position of your fireball image) You can add the if statement wherever in the update statement though i do mine towards the end \$\endgroup\$
    – Spooks
    Feb 15, 2011 at 23:00
  • \$\begingroup\$ I mean in which function of the class and do I also add a rectangle for my hero? Also which function for the update cause i have a hereo, enemy, spirte, and fireball calss along wiht the game1 class. \$\endgroup\$
    – user5476
    Feb 15, 2011 at 23:21
  • \$\begingroup\$ Hey Bradley, I explicitly told you to make a rectangle in both your hero class and fireball class, however you could also add one in your enemey class. If your hero, enemey and fireball all derive from the same class, then you could add it in that base class instead, make sure that you update the position of the rectangle every frame (you have todo this in the update or draw method of the entity that has the rectangle). Now the collision checking part needs to be done somewhere where you have access to all the items, usually in the game1.Update(..) method. \$\endgroup\$
    – Roy T.
    Feb 16, 2011 at 7:04
  • \$\begingroup\$ Thank you again for the help. Finally got it after thinking aobut it yet again. I had to add a rectangle for the player that was for his fireball and him, then I added a rectangle for the enemy and his fireball. Of course every update I had if jumping or moving and for the firball being shot. Then in the game update of game1.cs I just added an if statement to test it and then it would close the game if it ever became true. \$\endgroup\$
    – user5476
    Feb 17, 2011 at 0:08

You must log in to answer this question.