How to Make a Platform Game Like Super Mario Brothers – Part 2

This is a blog post by iOS Tutorial Team member Jacob Gundersen, an indie game developer who runs the Indie Ambitions blog. Check out his latest app – Factor Samurai! Welcome back to our 2-part tutorial series on making a game like Super Mario! In the first part of the series, you learned how to […] By Jake Gundersen.

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.

The Pit of Doom

Now for the scenario where Koalio falls down a hole. In this case, you'll end the game.

As the code is now, it will crash. (Horrors!) The code throws a TMXLayer: invalid position error. That's where you need to intervene. This occurs in the getSurroundingTilesAtPosition: method when you call tileGIDAt:.

Add the following code in GameLevelLayer.m, in the getSurroundingTilesAtPosition: method, before the tileGIDat: line;

if (tilePos.y > (map.mapSize.height - 1)) {
    //fallen in a hole
    [self gameOver:0];
    return nil;
} 

This code will run the gameOver routine and abandon the process of building the tile array. You'll also need to abandon the process of looping through the tiles in checkForAndResolveCollisions. After the NSArray *tiles = [self getSurroundingTilesAtPosition:p.position forLayer:walls ]; line, add this block of code:

  if (gameOver) {
    return;
  }

You'll abandon that loop as well and avoid any crashing that might result from trying to run the routine with an incomplete array.

Build and run now. Find a pit to jump into, and . . . no crashing! The game ends as it should.

Winning!

Now, handle the case where your hero Koalio wins the game!

All you're going to do is monitor the X-position of the player and trigger the “win” condition when he crosses the threshold (which will be at the end of the level). This level is about 3400 pixels wide. You're going to trigger a “win” condition when the player gets to pixel 3130.

Add a new method in GameLevelLayer.m as follows:

-(void)checkForWin {
  if (player.position.x > 3130.0) {
    [self gameOver:1];
  }
}
-(void)update:(ccTime)dt {
  [player update:dt];
  
  [self handleHazardCollisions:player];
  [self checkForWin];
  [self checkForAndResolveCollisions:player];
  [self setViewpointCenter:player.position];
}

Build and run now. Navigate your hero Koalio through the level, and if you can make it to the end, you'll have this message:

Gratuitous Music and Sound Effects

You know what time it is - time for gratuitous music and sound effects!

Let'd dive right into it. Add this at the top of GameLevelLayer.m and Player.m:

#import "SimpleAudioEngine.h"

Then add this line to the init method of the GameLevelLayer.

	[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"level1.mp3"];

This gives you some nice gaming music. Thanks to Kevin Macleod of Incompetech.com for composing the music (Brittle Reel). He has tons of CC licensed music there!

Now add a jumping sound. Go to the Player.m and add the following to the update method inside the jumping code:

if (self.mightAsWellJump && self.onGround) {
    self.velocity = ccpAdd(self.velocity, jumpForce);
    [[SimpleAudioEngine sharedEngine] playEffect:@"jump.wav"];
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
    self.velocity = ccp(self.velocity.x, jumpCutoff);
}

Finally, play a sound when Koalio falls down a hole or when he hits a hazard. Do that in the gameOver method in GameLevelLayer.m:

-(void)gameOver {
  gameOver = YES;
  [[SimpleAudioEngine sharedEngine] playEffect:@"hurt.wav"];  
  CCLabelTTF *diedLabel = [[CCLabelTTF alloc] initWithString:@"You have Died!" fontName:@"Marker Felt" fontSize:40];
  diedLabel.position = ccp(240, 200);

Build and run, and enjoy your groovy new tunes.

And, that's it. You've written a platformer. You. Are. Awesome!

Where to Go From Here?

Here's the final project file with all of the code from the tutorial.

There are a lot more things that could have been covered: everything from enemy collisions and AI, to enhanced movement abilities (wall slide, double jump, etc.), to level design guidelines.

And speaking of that... great news about that!

The Platformer Game Starter Kit

I'm happy to announce that all of this and more will be included in the upcoming Platformer Game Starter Kit! Here's a quick video showing you the game you'll make in the Starter Kit:

Here's what you'll learn in the Platformer Game Starter Kit:

  • How to manage and load multiple levels
  • How to make a scrollable, unlockable level selection screen
  • How to integrate UIKit Storyboards with Cocos2D
  • How to efficiently use sprite sheets, animations, tilesets, and work with pixel art!
  • How to create a state machine to handle character/enemy animations and behaviors
  • More about how to make amazing and fun tile-based physics engines!
  • How to create an on-screen joystick and HUD
  • How to add iCade support
  • Level design for platformers
  • How to build Enemy AI and dynamic behaviors.
  • How to add an EPIC Boss fight!
  • Interviews with the developers of several top iOS platformer games with tips and tricks
  • . . . and lots, lots more!

If you're interested in the Platformer Game Starter Kit, make sure you're signed up for Ray's monthly newsletter - we'll announce it there when it comes out! :]

Update 4/8/13: At long last, the Platformer Game Starter Kit is now available! Check it out on the raywenderlich.com store.

In the meantime, don’t forget the resources recommended at the end of Part 1 of this tutorial.

I hope you enjoyed getting your physics on and are more inspired than ever about building your own platformer!


This is a blog post by iOS Tutorial Team member Jacob Gundersen, an indie game developer who runs the Indie Ambitions blog. Check out his latest app - Factor Samurai!

Jake Gundersen

Contributors

Jake Gundersen

Author

Over 300 content creators. Join our team.