Learn Game Programming with Sprite Kit

Korey Hinton -> Blog -> iOS -> Learn Game Programming with Sprite Kit

Game Programming

../../media/icon-spritekit.png

I recently helped build an universal game app in Swift and Sprite Kit. I was quickly impressed with how quickly you can get a game up and running. While Sprite Kit differs from UIKit they do share some commonalities, so it felt familiar and you can actually add UIViews along side your sprites. If you are looking to get into game programming, Sprite Kit will be a great place to start.

Hello World

After downloading Xcode 6 beta I started a new Sprite Kit project. If you run the project immediately (CMD+R) you can see "Hello World" text drawn on the screen. The bottom-right of the screen has debugging text that shows the number of drawn nodes and the frames per second. If you tap on the screen a rotating Spaceship will be added to the screen.

../../media/firstrunwithspaceship.png

With so much functionality already built for you can easily start converting it into your own custom app. Instead of rotating you can quickly change it to move position instead.

Intro to the Sprite Kit API

Adding a sprite into the scene hierarchy is very similar to adding a view into the view hierarchy. In the scene you can simply add the node as a child.

var mySpriteNode = SKSpriteNode(image: "MyFileName.png")
self.addChild(mySpriteNode)

Adding a new image to the project is as easy as clicking on images.xcasset and then dragging your file next to the sprite file list. As you can see above it's 2 simple lines to get your sprite on screen. Obviously you may want to scale it or position it. You can use the properties it inherits from SKNode to accomplish this.

mySpriteNode.xScale = 0.5
mySpriteNode.yScale = 0.5
mySpriteNode.position = CGPointMake(200,200)

Sample Code

Apple actually built an adventure game that you can download the source code for. They also have a guide explaining the code. Visit this link to download the game and read their guide: code:Explained Adventure ../../media/adventure.png

I plan on adding additional posts about Sprite Kit. This is just an overview. You really can learn it just from the code provided by Apple. If Swift is new to you you may want to read the Swift Programming Language Guide.

Date: 2014-07-07T13:42+0000

Author: Korey Hinton

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0