Tutorial Cocos2d Create Your Simple Game – 7: Adding “Sound Effect” to your game

Screen Shot 2013-06-26 at 8.20.29 PM

Hello guys this tutorial was suppose to be about parallax loop, but I changed my mind, so I will do this simple tutorial about “How to add sound in your cocos2d game”, this tutorial shall be till number 10, but next week I plan to start a new serie of tutorial exploring a “simple platform game” for actionscript 3.

Well for now let’s start this one.

If you are following this tutorial from the beginning it would be easier, if not, you can try to understand the main code to implement the sound to your game (it is not hard at all). If you want to check the last tutorial “Tutorial Cocos2d Create Your Simple Game – 6: Adding Pause Game” click here.

First of all let’s begin downloading our mp3 sound of “Pew pew”. HERE. and DOWNLOAD IT! the download link will be bellow the media play bar.

Ok, it is not a true pew pew, but it will be “ok” for what we are suppose to do. 🙂

Drag the file lasershot.mp3 to your Resources folder (inside of your XCode).

This screen bellow is suppose to pop out, just click in finish and our lasershot.mp3 will be inside Resources.

Screen Shot 2013-06-26 at 7.51.48 PM

Now, lets import the “SimpleAudioEnngine.h”, go to Game.m, the places where the main game codes are.

and add this import line at the top of the code:

#import “SimpleAudioEngine.h”

Ok, now let’s preload the sound go our -(id) init{ (yet inside of the Game.m.

and inside of the: if((self=[super init])){ “ and add the preload code bellow:

[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@”lasershot.mp3″];

Ok, now we have the audio preloaded, so let’s play it.

Go to the method where call the shoot “-(void) addShoot:(id) sender{” and put it inside :

        [[SimpleAudioEngine sharedEngine] playEffect:@”lasershot.mp3″];

okay, now we have our “Pew pew” sound effect.

you can play and test it, if it don’t play the sound, check the plug of your headset :3

or

check if the lasershot.mp3 is trully inside of Resources and the filename is the same in the code.

😉

The entire code of the Game.m following bellow, the other files have no change so if you want see the entire game you must begin from the “Tutorial Cocos2d Create Your Simple Game – 2”, or wait till the tutorial number 10 where I plan to post all files. 😉

Screen Shot 2013-06-26 at 8.15.44 PM

Screen Shot 2013-06-26 at 8.16.05 PM

Screen Shot 2013-06-26 at 8.16.20 PM

Screen Shot 2013-06-26 at 8.16.33 PM

Screen Shot 2013-06-26 at 8.16.44 PM

Today it is all guys, next tutorial I will add some more sounds background and a background image running in our game. 😉

See ya.

 

2 thoughts on “Tutorial Cocos2d Create Your Simple Game – 7: Adding “Sound Effect” to your game

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s