Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have a 2D spaceship that shoots at moving rocks. If a bullet intersects one of the rocks' bounding boxes, that rock will animate according to a new spritesheet and remove itself.

My question is this: How can I load the new animated rock (breaking apart) onto the screen upon colliding with the bullet?

share|improve this question

tl;dr: You'll want to use a sprite sheet and create an animated sprite with a delegate to notify the owner when the animation has completed.

Let's break down the steps and design this out.

User Story

When a bullet collides with an asteroid, the asteroid should explode and disappear.

Feature Breakdown

  1. Detect collision between Bullet and Asteroid
  2. Play destroy animation on Asteroid's sprite.
  3. Remove Asteroid

Class Breakdown

  • AnimatedSprite: Displays an animated Sprite at a given position onscreen at a given rate
  • Hitbox: Defines an object's collidable area, and can be used to determine if two hitboxes collide
  • Bullet: Projectiles shot by the player containing a Sprite and a Hitbox
  • Asteroid: Obstacles to be shot by the player containing an AnimatedSprite and a Hitbox


I'm assuming you already have solved 1. Detect collision between Bullet and Asteroid, so let's jump to step 2.

There is a fairly comprehensive article regarding Sprite animation on MSDN that you should read before continuing development, but I'll try to sum it up here.

We're going to be using a sprite sheet for the asteroid's animation. The reasoning here is that a sprite sheet has much less memory overhead than discrete images (due to graphics card implementation and other stuff that you don't really need to worry about for this example).

The basic premise is to create a rectangle and only render that portion of the image. You can think of it as a film reel: There are many individual frames to create the motion, but you only see one at a time. Each update loop, a timer in the AnimatedSprite will be increased, and once it reaches the desired frame-rate, we will move the rectangle to the next part of the animation.

We'll want to modify the sample code above, adding a function, Play() and a delegate, AnimationComplete. Additionally, we'll only want to play the animation once, stop our animation, and call AnimationComplete. Then, in Asteroid, we'll want to pass a function, Destroy() to the delegate we created in AnimatedSprite. This means, once the animation is complete, the AnimatedSprite will call Destroy() on the asteroid (which is what we're trying to do).


Here's the task breakdown:

  • Detect collision between Bullet and Asteroid
  • Create an AnimatedSprite class to handle our animation
    • Should begin paused on the first frame
    • Should be able to begin playback via a function call
    • Should call a delegate once the animation is complete
  • Create a function to allow Asteroid to destroy itself
share|improve this answer

I think that the easiest solution is to load all images you need (and store it for example in an array) - I except you have animation stored as a lot of images. All time (while it's not colliding) you are drawing first image.

When it collides, you just start to use other sprites (you can draw for example one new sprite each frame). When you reach end of array, explosion (animation) is finished and you will stop drawing this rock.

share|improve this answer
Ah, I see. So, the new sprite will be drawn on top of the original. Correct? – Tomy Mar 12 '12 at 8:36
Not exactly. Because you are cleaning screen every frame (I hope you do this by calling GraphicsDevice.Clear in the beginning of your draw function), you will draw just one sprite. The first few frames first sprite will be drawn. When collision appears, each frame new sprite will be drawn. But none of previous sprites. – zacharmarz Mar 12 '12 at 10:36
You should take a look at some animation samples (for example, the platformer sample). Basically @zacharmarz description is correct - you use a index/reference to the sprite to draw each frame, and switch sprites as necessary due to animation or asteroid status. I would recomend you implement a single continuous animation for the objects before trying to switch animations due to game events. – SkimFlux Mar 12 '12 at 12:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.