Problem :
1. In this chapter, you built an example where two XNAlogo images moved
around the screen and bounced off the edges. Take the animated sprite example
that you built at the end of this chapter and make the animated sprite move and
bounce in a similar fashion—but in this case, make the animated sprite move in
both X and Y directions and bounce off of all four edges of the screen.3
This exercise is fairly straightforward, as it involves taking two examples
from the chapter and merging pieces together. The animation aspect of the
exercise is covered at the end of the chapter, while the movement and
bouncing off screen edges is covered in the middle of the chapter.Chapter 2: Fun with Sprites | 427
One thing that may be tricky is moving in both X and Y directions at the
same time. In the previous moving example, you used a float variable for the
speed and added it to either the X or the Y coordinate depending on which
sprite you were moving (they both moved in only one direction).
Now I’d recommend using a Vector2 to represent speed. That way you can
have different values for speed in the X direction and the Y direction. Also,
you can add your Vector2 speed to your Vector2 position by simply adding
the two together (you can add Vector2 objects just as you would add integer
or float values).
The other catch to this exercise is that in the previous moving example you
used the size of the Texture2D object to determine how far from the edge of
the screen to bounce in the other direction, but now the size of your
Texture2D object will be inaccurate because you’re using a sprite sheet.
Instead, you’ll want to use the size of an individual frame within that sprite
sheet to detect when the image has hit the edge of the screen.
Here is one possible solution for this exercise:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
- Code:
namespace AnimatedSprites
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;
Point frameSize = new Point(75, 75);
Point currentFrame = new Point(0, 0);
Point sheetSize = new Point(6, 8);
//Framerate stuff
int timeSinceLastFrame = 0;
int millisecondsPerFrame = 16;
//Speed and movement
Vector2 speed = new Vector2(5, 2);
Vector2 position = Vector2.Zero;
public Game1()428 | Appendix: Answers to Quizzes and Exercises
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>(@"imageshreerings");
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed)
this.Exit();
//Update time since last frame and only
//change animation if framerate expired
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
++currentFrame.Y;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
}
}
//Move sprite
position += speed;
//If the sprite hit a wall, reverse direction
if (position.X > Window.ClientBounds.Width - frameSize.X ||Chapter 3: User Input and Collision Detection | 429
position.X < 0)
speed.X *= -1;
if (position.Y > Window.ClientBounds.Height - frameSize.Y ||
position.Y < 0)
speed.Y *= -1;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.FrontToBack, SaveStateMode.None);
spriteBatch.Draw(texture, position,
new Rectangle(currentFrame.X * frameSize.X,
currentFrame.Y * frameSize.Y,
frameSize.X,
frameSize.Y),
Color.White, 0, Vector2.Zero,
1, SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}
}
}