Adding More Sound to Your Game
Let's take a minute now and add another sound feature to your XNA game. Close the SimpleSounds project and open the AnimatedSprites project you used at the beginning of this chapter.
In the game that you're building, a user-controlled sprite will be moving around the screen, with the objective of avoiding the automated sprites that are flying in from all directions. (That's right; plunk your money down now, this is going to be one amazing game.) You're moving along in that direction, and you'll get there soon enough.
Even though the automated sprites in the game currently don't move, you can still add some code to play a sound effect whenever your user-controlled sprite collides with an automated sprite.
You'll be passing the name of a cue to be played in the event of a collision into
each Sprite
object, so you'll first need to open
your Sprite.cs file and add to the Sprite
class a class-level variable that will hold the name of the
cue to be used. In addition, you'll need to use the auto-implemented properties
feature of C# 3.0 to create a public get
accessor
and a protected set
accessor for this
variable:
public string collisionCueName { get; private set; }
If you're new to C# 3.0 and are unfamiliar with this feature, auto-implemented properties allow developers to create accessors for a given variable at the point in code where the variable is declared. This streamlines the code, making it easier to implement and read. (Feel free to read up on auto-implemented ...
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.