O'Reilly logo

Learning XNA 3.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modifying the UserControlledSprite Class

Next, let's work on changes to the UserControlledSprite class. First, change the namespace from AnimatedSprites to Catch:

namespace Catch

When you worked on the 2D game using these classes in previous chapters, you were dealing with a one-player game and the score was kept in the Game1 class. You're now dealing with a two-player game. So, you'll need to either add a second score variable to the Game1 class or figure out a better solution. Because a UserControlledSprite represents a player, it would make sense to add the score to this class. Add the following class-level variable to the UserControlledSprite class:

public int score { get; set; }

Also, as mentioned earlier, you're going to be swapping players back and forth between the chasing sprite and the chased sprite. That means you'll need to add a variable that will keep track of which role this particular player sprite is currently playing:

public bool isChasing { get; set; }

Then, modify both constructors of the UserControlledSprite class to receive the chasing parameter. Also add code in the bodies of both constructors to initialize the isChasing and score variables:

public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, bool isChasing) : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, null, 0) { score = 0; this.isChasing = isChasing; } public UserControlledSprite(Texture2D ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required