In this final part of the tutorial series, you'll finish your game like Mega Jump, and add the level design, accelerometer support, and HUD using Swift and Sprite Kit!
Hello! The Swift language has changed some since this tutorial was written. The iOS SDK has been audited for nullability. Those parameters which are implicitly unwrapped in the code from this tutorial, are optionals now. Here’s the updated code:
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!) { data, error in
guard data != nil else {
print("There was an error: \(error)")
return
}
let acceleration = data!.acceleration
self.xAcceleration = (CGFloat(acceleration.x) * 0.75) + (self.xAcceleration * 0.25)
}
Since you’re now using Swift 2, the guard syntax is used to handle any errors.
Here’s what the same bit of code looks like using Swift 3:
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { data, error in
guard data != nil else {
print("There was an error: \(error)")
return
}
let acceleration = data!.acceleration
self.xAcceleration = (CGFloat(acceleration.x) * 0.75) + (self.xAcceleration * 0.25)
}
I hope this helps until we can update this tutorial.
Hi skyrocketsw, Your article helped me to alot in understanding sprite kit. I have a question.
In this part , i.e
if player.position.y > 200.0
{
backgroundNode.position = CGPoint(x: 0.0, y: -((player.position.y - 200.0)/10))
midgroundNode.position = CGPoint(x: 0.0, y: -((player.position.y - 200.0)/4))
foregroundNode.position = CGPoint(x: 0.0, y: -(player.position.y - 200.0))
} We are updating all background , mid ground and foreground nodes.
The player node position is not smooth when the update takes place.(On update player node position is not smooth it acts as glitch). Please help me out to solve this glitch .
Good afternoon, skyrockets! Really need help to make a game like mega jump, the game works fine, but when i go to xcode8 and Swift 3 game stopped working. The game shows the error:
2016-09-17 18:01:52.458231 Little story[558:84710] [DYMTLInitPlatform] platform initialization successful 2016-09-17 18:01:54.918116 Little story[558:84636] Metal GPU Frame Capture Enabled 2016-09-17 18:01:54.919299 Little story[558:84636] Metal API Validation Enabled fatal error: unexpectedly found nil while unwrapping an Optional value 2016-09-17 18:01:55.380419 Little story[558:84636] fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
code:
let levelPlist = Bundle.main.path(forResource: "Level01", ofType: "plist")
let levelData = NSDictionary(contentsOfFile: levelPlist!)!
endLevelY = (levelData["EndY"]! as AnyObject).intValue!
let platforms = levelData["Stars"] as! NSDictionary
let platformPatterns = platforms["Patterns"] as! NSDictionary
let platformPositions = platforms["Positions"] as! [NSDictionary]
for platformPosition in platformPositions {
let patternX = (platformPosition["x"] as AnyObject).floatValue
let patternY = (platformPosition["y"] as AnyObject).floatValue
let pattern = platformPosition["pattern"] as! NSString
let platformPattern = platformPatterns[pattern] as! [NSDictionary]
for platformPoint in platformPattern {
let x = (platformPoint["x"] as AnyObject).floatValue
let y = (platformPoint["y"] as AnyObject).floatValue
let type = PlatformType(rawValue: (platformPoint["type"]! as AnyObject).intValue)
let positionX = CGFloat(x! + patternX!)
let positionY = CGFloat(y! + patternY!)
let platformNode = createPlatformAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)
foregroundNode.addChild(platformNode)
}
}
let stars = levelData["Stars"] as! NSDictionary
let starPatterns = stars["Patterns"] as! NSDictionary
let starPositions = stars["Positions"] as! [NSDictionary]
for starPosition in starPositions {
let patternX = (starPosition["x"] as AnyObject).floatValue
let patternY = (starPosition["y"] as AnyObject).floatValue
let pattern = starPosition["pattern"] as! NSString
let starPattern = starPatterns[pattern] as! [NSDictionary]
for starPoint in starPattern {
let x = (starPoint["x"] as AnyObject).floatValue
let y = (starPoint["y"] as AnyObject).floatValue
let type = StarType(rawValue: (starPoint["type"]! as AnyObject).intValue)
was code: let levelPlist = NSBundle.mainBundle().pathForResource(“Level01”, ofType: “plist”)
let levelData = NSDictionary(contentsOfFile: levelPlist!)!
endLevelY = levelData["EndY"]!.integerValue!
let platforms = levelData["Platforms"] as! NSDictionary
let platformPatterns = platforms["Patterns"] as! NSDictionary
let platformPositions = platforms["Positions"] as! [NSDictionary]
for platformPosition in platformPositions {
let patternX = platformPosition["x"]?.floatValue
let patternY = platformPosition["y"]?.floatValue
let pattern = platformPosition["pattern"] as! NSString
let platformPattern = platformPatterns[pattern] as! [NSDictionary]
for platformPoint in platformPattern {
let x = platformPoint["x"]?.floatValue
let y = platformPoint["y"]?.floatValue
let type = PlatformType(rawValue: platformPoint["type"]!.integerValue)
let positionX = CGFloat(x! + patternX!)
let positionY = CGFloat(y! + patternY!)
let platformNode = createPlatformAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)
foregroundNode.addChild(platformNode)
}
}
let stars = levelData["Stars"] as! NSDictionary
let starPatterns = stars["Patterns"] as! NSDictionary
let starPositions = stars["Positions"] as! [NSDictionary]
for starPosition in starPositions {
let patternX = starPosition["x"]?.floatValue
let patternY = starPosition["y"]?.floatValue
let pattern = starPosition["pattern"] as! NSString
let starPattern = starPatterns[pattern] as! [NSDictionary]
for starPoint in starPattern {
let x = starPoint["x"]?.floatValue
let y = starPoint["y"]?.floatValue
let type = StarType(rawValue: starPoint["type"]!.integerValue)
let positionX = CGFloat(x! + patternX!)
let positionY = CGFloat(y! + patternY!)
let starNode = createStarAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)
foregroundNode.addChild(starNode)
}
}
// Load the level
let levelPlist = Bundle.main.path(forResource: “Level01”, ofType: “plist”)
let levelData = NSDictionary(contentsOfFile: levelPlist!)!
// Height at which the player ends the level
endLevelY = (levelData["EndY"]! as AnyObject).intValue!
// Add the platforms
let platforms = levelData["Platforms"] as! NSDictionary
let platformPatterns = platforms["Patterns"] as! NSDictionary
let platformPositions = platforms["Positions"] as! [NSDictionary]
for platformPosition in platformPositions {
let patternX = platformPosition["x"] as! Float
let patternY = platformPosition["y"] as! Float
let pattern = platformPosition["pattern"] as! NSString
// Look up the pattern
let platformPattern = platformPatterns[pattern] as! [NSDictionary]
for platformPoint in platformPattern {
let x = platformPoint["x"] as! Float
let y = platformPoint["y"] as! Float
let type = PlatformType(rawValue: platformPoint["type"]! as! Int)
let positionX = CGFloat(x + patternX)
let positionY = CGFloat(y + patternY )
let platformNode = createPlatformAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)
foregroundNode.addChild(platformNode)
}
}
// Add the stars
let stars = levelData["Stars"] as! NSDictionary
let starPatterns = stars["Patterns"] as! NSDictionary
let starPositions = stars["Positions"] as! [NSDictionary]
for starPosition in starPositions {
let patternX = starPosition["x"] as! Float
let patternY = starPosition["y"] as! Float
let pattern = starPosition["pattern"] as! NSString
// Look up the pattern
let starPattern = starPatterns[pattern] as! [NSDictionary]
for starPoint in starPattern {
let x = starPoint["x"] as! Float
let y = starPoint["y"] as! Float
let type = StarType(rawValue: starPoint["type"]! as! Int)
let positionX = CGFloat(x + patternX)
let positionY = CGFloat(y + patternY )
let starNode = createStarAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)
foregroundNode.addChild(starNode)
}
}
Updated with this. And run. Hope you won’t get that error.
Thanks for your great Tutorial.
I’m facing one issue when I tried to port the game to the newest version of Xcode. I have no idea why the collision part not working at all. Hope you can help me figure this out. Thanks.
This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]