Sprite Kit Tutorial: How to Make a Platform Game Like Super Mario Brothers – Part 2

Learn how to make a platform game like Super Mario Brothers in this Sprite Kit tutorial! 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 TMXLayerInfo assertion error. That's where you need to intervene. This occurs in the checkForAndResolveCollisionsForPlayer:forLayer: method when you call tileGIDAtTileCoord:.

Add the following code in GameLevelScene.m, in the checkForAndResolveCollisionsForPlayer:forLayer: method, after the CGPoint playerCoord = [layer coordForPoint:player.desiredPosition]; line;

if (playerCoord.y >= self.map.mapSize.height - 1) {
  [self gameOver:0];
  return;
}

This code will run the gameOver routine and abandon the process of building the tile array. You'll abandon the collision loop as well and avoid any issues that might result from it.

The handleHazardCollisions also loops through the tiles. You need to add some code to that method to prevent it from doing so if the Koala has jumped down a hole. Add the following line to the beginning of that method:

if (self.gameOver) return;

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

IMG_2651

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 GameLevelScene.m as follows:

-(void)checkForWin {
  if (self.player.position.x > 3130.0) {
    [self gameOver:1];
  }
}

Add the checkForWin method after the handleHazardCollisions call in update:

[self checkForWin];

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:

IMG_2652

Gratuitous Music and Sound Effects

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

Let'd dive right into it. You'll be using the built in Sprite Kit methods for sound effects and the SKTAudio engine for the background music. Add this at the top of GameLevelScene.m:

#import "SKTAudio.h"

Then add this line to the initWithSize method of the GameLevelScene (before self.userInteractionEnabled = YES;):.

[[SKTAudio sharedInstance] 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 = CGPointAdd(self.velocity, jumpForce);
  [self runAction:[SKAction playSoundFileNamed:@"jump.wav" waitForCompletion:NO]];
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
  self.velocity = CGPointMake(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 GameLevelScene.m:

-(void)gameOver:(BOOL)won {
  self.gameOver = YES;
  [self runAction:[SKAction playSoundFileNamed:@"hurt.wav" waitForCompletion:NO]];
  
  NSString *gameText;

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 SuperKoalioPt2Fin with all of the code from this Sprite Kit 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

We just released the second edition of the Platformer Game Starter Kit, which is fully updated for Sprite 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 easily Sprite Kit works with UIKit Storyboards
  • 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 hardware controller support
  • Level design for platformers
  • How to build Enemy AI and dynamic behaviors.
  • Interviews with the developers of several top iOS platformer games with tips and tricks
  • . . . and lots, lots more!

You can check out the second edition of the Platformer Game Starter Kit 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!

Jake Gundersen

Contributors

Jake Gundersen

Author

Over 300 content creators. Join our team.