Using Labels in SpriteKit with Swift

With Sprite Kit you can display text in your game using Labels. These labels are of type SKLabelNode and its main function is to display a string. In this tutorial we will display a label and add the functionality to drag it around the screen. This tutorial is built for iOS 8.1 and Xcode 6.1.

Open Xcode and create a new Project. Select the Game template.

For product name, use SpriteKitSwiftLabelTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

e will add a custom background so download the image and add it to the project. Make sure you select the "Copy items if needed" option

The Game template added some code to the project we will not need. Go to the GameScene.swift file and delete the touchesBegan(touches:withEvent) method. Also delete all code inside the didMoveToView method. Add the following properties to the GameScene class.

var deltaPoint = CGPointZero
var myLabel:SKLabelNode!

The deltaPoint property is needed for recording changes in the touchlocation while dragging the label. The myLabel is of type SKLabelNode, which is a node that draws a string. Next, we will add the background image. Change the didMovetoView method to

override func didMoveToView(view: SKView) {
      var bgImage = SKSpriteNode(imageNamed: "wallpaper.png")
      bgImage.position = CGPointMake(self.size.width/2, self.size.height/2)

The background image is centered and added to the GameScene. Next add the following lines to the end of the didMovetoView method

myLabel = SKLabelNode(fontNamed: "Arial")
myLabel.text = "Drag this label"
myLabel.fontSize = 20
myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

Ee create a label with font-type of Arial and a font size of 20. Our label is centered to the screen and added to the scene. Next, we will need to capture the player's touches so add the following methods.

override func touchesMoved(touches: NSSet, withEvent: UIEvent) {
      var currentPoint:CGPoint! = touches.anyObject()?.locationInNode(self)
      var previousPoint:CGPoint! = touches.anyObject()?.previousLocationInNode(self)
      deltaPoint = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y)
override func touchesEnded(touches: NSSet, withEvent: UIEvent) {
      deltaPoint = CGPointZero
override func touchesCancelled(touches: NSSet!, withEvent: UIEvent!) {
      deltaPoint = CGPointZero

The touchesMoved:withEvent method sets the currentPoint variable to the current position of the player finger and previousPoint to the previous position. It then calculates the difference. When the touch is ended or canceled no touch is onscreen so the deltaPoint is zero. To update the position of the label we need to change the update method to

override func update(currentTime: CFTimeInterval) {
      var newPoint = CGPointMake(self.myLabel.position.x + self.deltaPoint.x, self.myLabel.position.y + self.deltaPoint.y)
      myLabel.position = newPoint
      deltaPoint = CGPointZero

We calculate the new position by adding the current deltaPoint value from our touch methods. When the label has repositioned we set the deltaPoint back to zero. Buid and Run the project and drag the label around the screen.

ou can download the source code of the SpriteKitSwiftLabelTutorial at the ioscreator repository on github.