Test Your Knowledge: Exercise

  1. Modify the code that you worked on this chapter to create four sprites that move and bounce off all four edges of the screen. To accomplish this, create a new class called BouncingSprite which derives from AutomatedSprite. BouncingSprite should do the same thing that AutomatedSprite does with the exception that it will check during the Update method to determine if the sprite has gone off the edge of the screen. If it has, reverse the direction of the sprite by multiplying the speed variable by −1.

    Also, make two of the bouncing sprites use the skull image and two of them use the plus image (located with the source code for this chapter in the AnimatedSprites\AnimatedSprites\Content\Images directory).

    Note that when running this game after making these changes you'll have four sprites moving around the screen and the game will exit when any of them collide with the user controlled sprite. This may cause some issues in testing the game because the sprites may be colliding when the game first loads. Try moving your mouse to a far corner of the screen when loading the game to get your user controlled sprite out of the way to begin with.

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.