XNA 101 .Net

Learn how to program in C# while writing games in XNA Game Studio 3.0!
Our upcoming Silverlight book for beginners (includes a great chapter on game development in Silverlight!) Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

Recent posts

Navigation

Archive

Blogroll

    Tampa Divorce Lawyer

    North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    Lesson 13: The Delicate Sound of Thunder

    Sounds are something that got a lot easier in XNA 3.0. You used to have to create an XACT project, add your sounds to that, and then add your XACT project to your XNA project. Then there were a couple of hoops you had to jump through in your code to make it work. You can still do sounds the old way, and in some cases especially when doing sound mixing, you may need to, but for simple sound effects the technique shown in the sample is quick and easy.

    This lesson begins where Lesson 12 leaves off.

    Lesson 13 Completed Source Code: http://www.bluerosegames.com/xna101/lessons/XnaLesson13.zip

    Sound effects are so important to game development. You can do a great sprite animation for an explosion, but without the KABOOM! it loses a lot of its impact.

    For this sample, we?re going to use a free sound from Flashkit.com, a great resource for free sounds. Another great place is A1 Free Sound Effects: http://www.a1freesoundeffects.com/noflash.htm

    From Flashkit.com, download the following:

    http://www.flashkit.com/downloads/soundfx/wav/7730/Boing.zip

    Extract the sound file Boing-Sith_Mas-479.wav and add it to your Content folder.

    XNA 3.0 provides the SoundEffect class to make it easy to play sound effects. This class is located in the Microsoft.Xna.Framework.Audio namespace, so make sure your BouncingBall.cs and Game1.cs files have the following using statement at the top:

    using Microsoft.Xna.Framework.Audio;  

    Let?s declare a static field in the BouncingBall class to hold the sound effect:

    static public SoundEffect Boing;

    Then like we did previously for textures, we?ll do a Content.Load in the Game1.LoadContent method:

    BouncingBall.Boing = Content.Load<SoundEffect>("Boing-Sith_Mas-479");

    Now that the sound effect is loaded, it?s time to figure out when to play the sound. In the BouncingBall.Update() method, add this declaration to the beginning of the method:

    bool bounced = false;

    This declares a boolean, or true and false value, that we can use as a flag to determine whether a bounce took place. As part of the declaration, we are setting its initial value to false. It will keep that value until the end of the method unless it is changed.

    So now inside the both of the two conditional "if" blocks, add the following statement:

    bounced = true;

    and lastly, at the end of the method, if bounced is true, meaning we hit a wall, we want to play the sound. So add the following lines:

       1: if (bounced == true) 
       2: { 
       3:    Boing.Play(); 
       4: } 

    In C#, the double equals sign in the "if" conditional statement means we are doing a comparison, not an assignment. If we used a single equals sign there, the code would think we wanted to set bounced to true instead of checking to see if it was true, and so the conditional would always be true, and the sound would always play.

    That?s all there is to it. Run the program again. If all went well, you should get a boing sound every time a ball hits a wall.

    Posted: Nov 11 2008, 13:27 by Bill Reiss | Comments (4) RSS comment feed |
    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Filed under:

    Comments

    Add comment


     

      Country flag

    biuquote
    • Comment
    • Preview
    Loading