Book Image

Sparrow iOS Game Framework Beginner's Guide

By : Johannes Stein
Book Image

Sparrow iOS Game Framework Beginner's Guide

By: Johannes Stein

Overview of this book

Table of Contents (20 chapters)
Sparrow iOS Game Framework Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Afterword
Index

Time for action – playing music in our scenes


Perform the following steps to play background music in our scenes:

  1. Open the Scene.h file.

  2. Add an instance variable named backgroundMusic, which is a pointer to SPSoundChannel using the following line of code:

    SPSoundChannel *backgroundMusic;
  3. Declare a method called stop as follows:

    -(void) stop;
  4. Inside the Scene.m file, define the stop method with an empty body.

  5. Update the showScene method in the SceneDirector.m file to fit the following block of code:

    -(void) showScene:(NSString *)name
    {
        for (NSString* sceneName in _dict) {
            ((Scene *) _dict[sceneName]).visible = NO;
            [((Scene *) _dict[sceneName]) stop];
        }
        if (_dict[name] != nil) {
            ((Scene *) _dict[name]).visible = YES;
            [((Scene *) _dict[name]) reset];
            
        }
    } 
  6. Switch to PirateCove.m.

  7. Inside the initializer, add the following lines at the top:

    SPSound *sound = [Assets sound:@"music_cove.aifc"];
    backgroundMusic = [sound createChannel];
    backgroundMusic...