How To Make A Game Like Fruit Ninja With Box2D and Cocos2D – Part 3

This is a post by iOS Tutorial Team Member Allen Tan, an iOS developer and co-founder at White Widget. You can also find him on Google+ and Twitter. Welcome to the third part of a tutorial series that shows you how to make a sprite cutting game similar to the game Fruit Ninja by Halfbrick […] By Allen Tan.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Gratuitous Music and Sound Effects

You know it wouldn't be a raywenderlich.com game tutorial without fun music and sound effects! :]

Our sound effects will not only help set the mood, they will also help the player distinguish the different game events.

So go ahead and add the Sounds folder from the resources folder into your Xcode project. This contains sounds for the following events:

  • A bomb explodes
  • A bomb is tossed
  • Fruits are tossed consecutively
  • Fruits are tossed simultaneously
  • The player loses a life
  • The player cuts a fruit and blobs jump out
  • The player cuts a fruit repeatedly
  • The player swipes
  • Nature sounds in the background

Switch to HelloWorldLayer.h and make these changes:

// Add to top of file
#import "SimpleAudioEngine.h"

// Add inside the @interface
float _timeCurrent;
float _timePrevious;
CDSoundSource *_swoosh;

// Add after the @interface
@property(nonatomic,retain)CDSoundSource *swoosh;

Switch again to HelloWorldLayer.mm, and make the following changes:

// Add inside @implementation
@synthesize swoosh = _swoosh;

// Add inside the dealloc method, before [super dealloc]
[_swoosh release];

// Add inside the init method
[[SimpleAudioEngine sharedEngine] preloadEffect:@"swoosh.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"squash.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"toss_consecutive.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"toss_simultaneous.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"toss_bomb.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"lose_life.caf"];
_swoosh = [[[SimpleAudioEngine sharedEngine] soundSourceForFile:@"swoosh.caf"] retain];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"nature_bgm.aifc"];
_timeCurrent = 0;
_timePrevious = 0;

// Add inside the update method
_timeCurrent += dt;

// Add inside the spriteLoop method, after tossing the bomb
[[SimpleAudioEngine sharedEngine] playEffect:@"toss_bomb.caf"];

// Add inside the spriteLoop method, for both the consecutive tosses
[[SimpleAudioEngine sharedEngine] playEffect:@"toss_consecutive.caf"];

// Add inside the spriteLoop method, for the simultaneous toss
[[SimpleAudioEngine sharedEngine] playEffect:@"toss_simultaneous.caf"];

// Add inside splitPolygon if sprite is a bomb
[[SimpleAudioEngine sharedEngine] playEffect:@"explosion.caf"];
 
// Add inside splitPolygon if sprite is not a bomb
[[SimpleAudioEngine sharedEngine] playEffect:@"squash.caf"];

// Add before destroying the body in the splitPolygonSprite method
[[SimpleAudioEngine sharedEngine] playEffect:@"smallcut.caf"];

// Add inside the subtractLife method
[[SimpleAudioEngine sharedEngine] playEffect:@"lose_life.caf"];

// Add inside ccTouchesMoved before setting _bladeSparkle.position = location
ccTime deltaTime = _timeCurrent - _timePrevious;
_timePrevious = _timeCurrent;
CGPoint oldPosition = _bladeSparkle.position;

// Add inside ccTouchesMoved after setting _bladeSparkle.position = location
if (ccpDistance(_bladeSparkle.position, oldPosition) / deltaTime > 1000)
{
    if (!_swoosh.isPlaying)
    {
        [_swoosh play];
    }
}

Aside from the usual sound playing code, you added a time factor to solve for the current velocity of the swipe based on a distance/time formula so that the swoosh sound effect only plays when the player swipes fast enough. You also maintain a reference to 1 swoosh sound effect, and only play it if it is not already playing.

You're done! Congratulations, you've just made a complete fruit cutting game for the iPhone!

Where To Go From Here?

Here is the sample project with the completed project from the tutorial series.

From here on out, you can work on improving the game. Here are some ideas for some fun things you could do to make the game even better:

  • Support concave polygons by using triangulation (splitting a concave polygon into several convex polygons).
  • Support polygons with more than 8 vertices by following the same method.
  • Update PolygonSprite to support batch nodes.
  • Support multiple touches and swipes.
  • Support the iPad.
  • Cache even the split polygons so that everything can be reused. This is sure to boost performance.
  • Add slice streaks and give bonus points to cutting sprites in sequence.
  • Add special events when certain fruits are cut.
  • More randomness and variety of tosses, like tossing some fruits from the sides.

If you have any ideas on how to make the game cooler, or if you have any concerns, questions, and comments on the tutorial, please join the forum discussion below!


This is a post by iOS Tutorial Team Member Allen Tan, an iOS developer and co-founder at White Widget. You can also find him on and Twitter.

Allen Tan

Contributors

Allen Tan

Author

Over 300 content creators. Join our team.