How to Make a Game Like Mega Jump With Sprite Kit and Swift: Part 1/2
In part one of this two-part tutorial series, you’ll start creating a game like Mega Jump, and implement the overall scene, physics, and sound effects using Swift and Sprite Kit! By Michael Briscoe.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Make a Game Like Mega Jump With Sprite Kit and Swift: Part 1/2
40 mins
- Getting Started
- Importing the Art
- Building the Scene
- Adding the Player Node
- Adding Gravity and a Physics Body
- Tap To Start
- Game Objects: Reach for the Stars!
- The Star Class
- Collision Handling and Bit Masks
- Multiple Star Types
- Ping! Adding a Sound Effect
- Game Objects: Platforms
- Where to Go From Here?
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 GameObjectNode.swift, add the following enumeration:
enum StarType: Int {
case Normal = 0
case Special
}
To store the star type, add the following property to the top of the StarNode
class:
var starType: StarType!
You now need to specify a star type when creating a star, so in GameScene.swift, add starType
as a parameter to the createStarAtPosition:
method signature so that it looks like this:
func createStarAtPosition(position: CGPoint, ofType type: StarType) -> StarNode {
Inside createStarAtPosition(position: ofType:)
, replace the three lines of code that create and add the SKSpriteNode
with the following:
node.starType = type
var sprite: SKSpriteNode
if type == .Special {
sprite = SKSpriteNode(imageNamed: "StarSpecial")
} else {
sprite = SKSpriteNode(imageNamed: "Star")
}
node.addChild(sprite)
First, you set the star type. Then, when you create the sprite, you check to see which star type it is so you can get the correct graphic.
All that remains is to specify a star type when you create the star. In init(size:)
in GameScene.swift, find the line where you call createStarAtPosition:
and replace it with this:
let star = createStarAtPosition(CGPoint(x: 160, y: 220), ofType: .Special)
You specify that you would like this star to be a StarType.Special
type.
Build and run. Your star is now pink! Later, you’ll add a scoring system and the differences in star types will become more apparent.
Ping! Adding a Sound Effect
It would be nice if, when the player collides with the star, they got an audible cue. Download the star sound effect here and drag it into your Xcode project. Make sure that “Destination: Copy items if needed” is checked and that your UberJump target is selected.
In Sprite Kit, you use SKAction
to play sounds. Open GameObjectNode.swift. At the top of StarNode
, add the following class property:
let starSound = SKAction.playSoundFileNamed("StarPing.wav", waitForCompletion: false)
Now all that remains is to run the sound action when the player node collides with the star.
Within collisionWithPlayer()
in StarNode
, replace self.removeFromParent()
with:
// Play sound
runAction(starSound, completion: {
// Remove this Star
self.removeFromParent()
})
This runs the SKAction
, plays the sound file, and removes the star when the action is complete.
Build and run. As the player sprite collides with the star, you’ll hear a delightful, twinkly ping. :]
Game Objects: Platforms
Your final task in this first part of the series is 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.
In GameObjectNode.swift, add the following enumeration above the GameObjectNode
class definition to define the two platform types:
enum PlatformType: Int {
case Normal = 0
case Break
}
Next you’ll create the PlatformNode
class. Add the following code to GameObjectNode.swift:
class PlatformNode: GameObjectNode {
var platformType: PlatformType!
override func collisionWithPlayer(player: SKNode) -> Bool {
// 1
// Only bounce the player if he's falling
if player.physicsBody?.velocity.dy < 0 {
// 2
player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 250.0)
// 3
// Remove if it is a Break type platform
if platformType == .Break {
self.removeFromParent()
}
}
// 4
// No stars for platforms
return false
}
}
Let’s take a closer look at this code:
- 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 itsvelocity
. This check also ensures the player node doesn't collide with platforms while moving up the screen. - 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?
- If this is a
platformType.Break
platform, then you remove the platform from the scene. - Finally, the Uber Jumper doesn’t get points from bouncing on or off platforms so there is no need to refresh the HUD.
To add a platform to your scene, open GameScene.swift and add the following method:
func createPlatformAtPosition(position: CGPoint, ofType type: PlatformType) -> PlatformNode {
// 1
let node = PlatformNode()
let thePosition = CGPoint(x: position.x * scaleFactor, y: position.y)
node.position = thePosition
node.name = "NODE_PLATFORM"
node.platformType = type
// 2
var sprite: SKSpriteNode
if type == .Break {
sprite = SKSpriteNode(imageNamed: "PlatformBreak")
} else {
sprite = SKSpriteNode(imageNamed: "Platform")
}
node.addChild(sprite)
// 3
node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.physicsBody?.dynamic = false
node.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Platform
node.physicsBody?.collisionBitMask = 0
return node
}
This method is very similar to createStarAtPosition(_:ofType:)
, but note these main points:
- You instantiate the
PlatformNode
and set its position, name and type. - You choose the correct graphic for the
SKSpriteNode
based on the platform type. - You set up the platform’s physics, including its collision category.
Now to add the platform, insert the following code in init(size:)
, just before the line that creates the star:
// Add a platform
let platform = createPlatformAtPosition(CGPoint(x: 160, y: 320), ofType: .Normal)
foregroundNode.addChild(platform)
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!
Where to Go From Here?
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.
You can download the Xcode 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!