Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page