Do you like Swift programming language come with XCode 6 from Apple? You may not know, but I like it after writing a game with it. I am going to show you how to write a game with Swift. If you would like to jump right into the source code of this game, you can download/clone from GitHub
Getting start with XCode 6
First of all you need to download XCode 6. At this moment XCode 6 is not official released, so you need to join ‘iOS Developer Program’ to download. After you have installed XCode and choose to Create a new Xcode project, you can create a iOS Application with Game template,
Fill in product name, choose language Swift and SpriteKit,
Type Inference
Once the canned game is created, you can set scaleMode in GameViewController.swift to .AspectFit, e.g.,scene.scaleMode = .AspectFit
The canned version sets scaleMode to .AspectFill. I got lost with the coordinates with .AspectFill and found part of the game scene isn’t being shown under this mode. I found .AspectFit would be easier to learn the coordinate system because all scene is shown. Empty spaces are added with .AspectFit if they do not match instead, so you will not miss any part of your scene. You do not need to reference type for .AspectFit as in objective-C, as it can deduce the type from scene.scaleMode with Type Inference. Also, the semicolon is not required to end a statement. Starting to love it?
Handling different scene aspect ratio
Next, resize scene in GameScene.sks to 640 width and 1136 height such that it fits aspect ratio to iPhone 5. The numbers are not in pixel unit because the scene could be scaled according to scaleMode, using 640 is easier for reference in the game program. It could be difficult to see the selected game scene because it fits the editor on the first launch, zooming out with the ‘-’ icon could help. You know you have selected the scene from breadcrumb or property window on right.
Build basic game elements with SpriteKit
SpriteKit is very familiar to me as I have experience with Cocos2d and Cocos2d-x. It is an all-in-one physics engine, 2d graphics engine, animation engine. Let’s start to create pins, fences and border
1 | import SpriteKit |
Non-optional/Optional Variable?
You may wonder what SKShapeNode? is in ‘var borderBottom: SKShapeNode? = nil’. Variables in Swift are non-optional variables and they are not allow to set nil (or null) by default. You have to declare the variable to optional variable by putting ? the end of variable type. This make Swift more ‘fool-proof’ because many developers in other language dereference variable by accident, causes many null pointer exception or calling method from undefined object. Having null annotation could be helpful but your code would filled with annotations. Long words, but we still have no game to play. Now lets try to add some action to have fun
1 | override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { |
The spaceships fills up the screen if you tap non-stop. Let’s do something when the spaceship hit the floor,
Handle object collision
First, make the GameScence to conforms to SKPhysicsContactDelegate, which means it could have a function to handle contact between physics objects
1 | class GameScene: SKScene, SKPhysicsContactDelegate { |
And below is the function to handle objects contact,
1 | func didBeginContact(contact: SKPhysicsContact!) { |
You should set contact delegate to the GameScene as it conforms to the protocol,
1 | override func didMoveToView(view: SKView) { |
Cool! Hope you are not addict to the game. Download/browe source in GitHub. Wish one day Swift and SpriteKit would become cross-platform.