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 – implementing a sound manager


To implement the sound manager, just follow these simple steps:

  1. Add a new Objective-C class called SoundManager derived from AssetsDictionary within the Assets group.

  2. Add an instance method that will register a sound using its filename and return the correct value, as shown in the following code:

    -(SPSound *) registerSound:(NSString *)filename;
  3. Implement the method from the previous step with the following content:

    -(SPSound *) registerSound:(NSString *)filename
    {
        if ([_dict objectForKey:filename] == nil) {
        return (SPSound *) [self registerAsset:filename withContent:[SPSound soundWithContentsOfFile:filename]];
      } else {
        return (SPSound *) [self registerAsset:filename withContent:nil];
      }
    }

What just happened?

In the first step, we created a SoundManager class, which is a subclass of AssetsDictionary. In step 2, we defined the registerSound method, which we implemented in the next step; this method loads a sound from file and returns...