How to Make a Game Like Mega Jump With Sprite Kit: Part 1/2

In this two-part tutorial series, you’ll learn how to create a game like Mega Jump using the latest and greatest game framework for iOS: Sprite Kit. By Toby Stephens.

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

Multiple Star Types

Uber Jump will have two different kinds of stars: those worth a single point and special stars worth five points. Each star-type will have its own graphic. To identify the star type, you’ll have an enumeration in the StarNode class.

At the top of StarNode.h, add the following enumeration:

Game Objects: Platforms

Where to Go From Here?

Build and run. As the player sprite collides with the star, you’ll hear a delightful, twinkly ping. :]

In the final section of the first part of this tutorial, you’re going to add a platform to the scene. You'll represent platforms with a new subclass of GameObjectNode so that you get all the associated goodness from that class. As with the stars, you’ll have two types of platforms: one that is indestructible and one that disappears as soon as the player sprite has jumped off of it.

Start by creating a new Objective-C class called PlatformNode and make it a subclass of GameObjectNode.

14-NewClassPlatformNode

In PlatformNode.h, add the following enumeration above the @interface section to define the two platform types:

Still in PlatformNode.h, add a property for the platform type to the interface:

In PlatformNode.m, add the following implementation of collisionWithPlayer::

Let’s take a closer look at this code:

To add a platform to your scene, open MyScene.m and import the PlatformNode header at the top, like so:

Now add the following method:

This method is very similar to createStarAtPosition:ofType:, but note these main points:

Now to add the platform, insert the following code in initWithSize:, just before the line that creates the star:

Build and run the game. Tap to start and watch as the player sprite gets a boost from the star and then bounces on the platform!

15-FirstPlatform

Well done! As you've seen, creating a game like Mega Jump is not that hard at all. You've got all the basics of Uber Jump in place and are on your way to building a great game.

Here is the project with everything you've done so far in this tutorial series.

In Part Two of the series, you’ll implement the accelerometer for movement on the x-axis. You’ll also load an entire level from a property list and add a scoring system. That’s a lot to look forward to, right? ;]

Please post a comment below if you have any questions, thoughts or suggestions for future tutorials!

  1. As is common in this type of game, the player node should only bounce if it hits a platform while falling, which is indicated by a negative dy value in its velocity. This check also ensures the player node doesn't collide with platforms while moving up the screen.
  2. You give the player node a vertical boost to make it bounce off the platform. You accomplish this the same way you did for the star, but with a less powerful boost. You want stars to be important, right?
  3. If this is a PLATFORM_BREAK-type platform, then you remove the platform from the scene.
  4. Finally, the Uber Jumper doesn’t get points from bouncing on or off platforms so there is no need to refresh the HUD.
  1. You instantiate the PlatformNode and set its position, name and type.
  2. You choose the correct graphic for the SKSpriteNode based on the platform type.
  3. You set up the platform’s physics, including its collision category.






typedef NS_ENUM(int, PlatformType) {
  PLATFORM_NORMAL,
  PLATFORM_BREAK,
};
@property (nonatomic, assign) PlatformType platformType;
- (BOOL) collisionWithPlayer:(SKNode *)player
{
  // 1
  // Only bounce the player if he's falling
  if (player.physicsBody.velocity.dy < 0) {
    // 2
    player.physicsBody.velocity = CGVectorMake(player.physicsBody.velocity.dx, 250.0f);

    // 3
    // Remove if it is a Break type platform
    if (_platformType == PLATFORM_BREAK) {
      [self removeFromParent];
    }
  }

  // 4
  // No stars for platforms
  return NO;
}
#import "PlatformNode.h"
- (PlatformNode *) createPlatformAtPosition:(CGPoint)position ofType:(PlatformType)type
{
  // 1
  PlatformNode *node = [PlatformNode node];
  [node setPosition:position];
  [node setName:@"NODE_PLATFORM"];
  [node setPlatformType:type];

  // 2
  SKSpriteNode *sprite;
  if (type == PLATFORM_BREAK) {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"PlatformBreak"];
  } else {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Platform"];
  }
  [node addChild:sprite];
	
  // 3
  node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
  node.physicsBody.dynamic = NO;
  node.physicsBody.categoryBitMask = CollisionCategoryPlatform;
  node.physicsBody.collisionBitMask = 0;

  return node;
}
// Add a platform
PlatformNode *platform = [self createPlatformAtPosition:CGPointMake(160, 320) ofType:PLATFORM_NORMAL];
[_foregroundNode addChild:platform];
typedef NS_ENUM(int, PlatformType) {
  PLATFORM_NORMAL,
  PLATFORM_BREAK,
};
@property (nonatomic, assign) PlatformType platformType;
- (BOOL) collisionWithPlayer:(SKNode *)player
{
  // 1
  // Only bounce the player if he's falling
  if (player.physicsBody.velocity.dy < 0) {
    // 2
    player.physicsBody.velocity = CGVectorMake(player.physicsBody.velocity.dx, 250.0f);

    // 3
    // Remove if it is a Break type platform
    if (_platformType == PLATFORM_BREAK) {
      [self removeFromParent];
    }
  }

  // 4
  // No stars for platforms
  return NO;
}
#import "PlatformNode.h"
- (PlatformNode *) createPlatformAtPosition:(CGPoint)position ofType:(PlatformType)type
{
  // 1
  PlatformNode *node = [PlatformNode node];
  [node setPosition:position];
  [node setName:@"NODE_PLATFORM"];
  [node setPlatformType:type];

  // 2
  SKSpriteNode *sprite;
  if (type == PLATFORM_BREAK) {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"PlatformBreak"];
  } else {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Platform"];
  }
  [node addChild:sprite];
	
  // 3
  node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
  node.physicsBody.dynamic = NO;
  node.physicsBody.categoryBitMask = CollisionCategoryPlatform;
  node.physicsBody.collisionBitMask = 0;

  return node;
}
// Add a platform
PlatformNode *platform = [self createPlatformAtPosition:CGPointMake(160, 320) ofType:PLATFORM_NORMAL];
[_foregroundNode addChild:platform];
Toby Stephens

Contributors

Toby Stephens

Author

Over 300 content creators. Join our team.