Game Development in Swift

by T. Urtti

Read this first

The Road To 1000 Downloads

Today is a day of celebration, the cumulative downloads of my 3 iOS games broke the one thousand mark arriving at 1005 downloads!

It is exactly 153 days since the release of the first game.

I don’t know how representative this is of an average indie developer’s experience, but it’s certainly very different than the numbers of Rovio or Supercell. Of course, in all honesty, my games are also less refined. Generally speaking I’m happy, as every one of my releases has racked up downloads faster than the previous ones, and more importantly I have received positive feedback from the gamers.

Below I have added the downloads graph so far. All the games have followed a very similar pattern. The downloads shoot up at release, but crash down within a couple days.

Alt text

Game of Roads was reviewed by 148apps ( which lead to a nice increase in

Continue reading →

Programmatic Background Gradient Layer

I saw an interesting post about gradient programmatically that was written in Objective-C:

I ported to Swift for my own use and it seems to work fine still!

import Foundation
import QuartzCore
import SpriteKit

class BackgroundLayer
    var greyGradient = CAGradientLayer()
    var blueGradient = CAGradientLayer()

        var colorOne = UIColor(white: 0.9, alpha: 1.0)
        var colorTwo = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.85, alpha: 1.0)
        var colorThree = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.7, alpha: 1.0)
        var colorFour = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.4, alpha: 1.0)
        var colors = [CGColor](arrayLiteral: colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor, colorFour.CGColor)

Continue reading →

Localizing SKLabelNodes in .sks files

Most of my first scenes I implemented by code instead of the GUI tool, but eventually I thought it could be quite helpful for certain scenes.

I quickly noticed the issue that it was not so straightforward to localize the SKLabelNode texts anymore.

I tried the default method by clicking ‘Localize…’ button in the tool, but that actually made duplicates of the .sks file that I was supposed to change for localizing the texts. This seemed to have the bad side that if I were to make changes to the layout or add new elements, I would have to do it multiple times.

Instead I implemented this bit of code where I unarchive the .sks files. This is what the default unarchive function more or less looks like:

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {

        let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")

        var sceneData =

Continue reading →

Multiplayer ELO score in Swift

I’m in the process of developing my first game for iOS. At first I wanted to include the so-called ELO score in the 2nd or 3rd version of the game, but this week my thoughts came around and now I believe that the competitiveness factor that leaderboards and a global ranking bring to the game should be a first release feature.

The Elo-score in short is a system that ranks players in comparison to their peers. Losing a game to a better player is not as bad as losing against a worse player. You also gain more by beating opponents that are better than you. Elo was created for duel games, so it remains to be seen how well it works here.

The design of the multiplayer Elo here is identical to the duel Elo, the only difference being that all players are compared to all their opponents. Please find below the code, hope it helps! I have only tested it shortly and it seems to be working fine for

Continue reading →

How swift is Swift compared to C++?

Merriam-Webster defines swift as “moving or capable of moving at great speed”. Apple also presented similar promises at their WWDC 2014 keynote presentation, where Swift was first released. In one of the slides there was a comparison for RC4 encryption that Swift did 220x faster than Python and almost 2x faster than Objective-C.

Alt text

What about the classical behemoth of performance C++? Which one of these two languages takes the cake when it comes to calculating the Collatz conjecture for numbers under 1 million? Let’s find out!

I made an effort to keep the solutions as similar as possible to have a fair comparison. First I will show you the full source code of both solutions and finally the results.

 C++ Solution

#include <iostream>
#include <sys/time.h>

int main(int argc, const char * argv[])
    // Take starting time for performance measurement
    struct timeval start, end;

Continue reading →

Calculating the Collatz conjecture with Swift

The Collatz conjecture is a mathematical conjecture named after Lothar Collatz. The basic idea is described in Wikipedia as the following:

“Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One”, or HOTPO[6]) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.“

So fundamentally very simple mathematics. So let’s get started by defining the range of natural numbers to test and creating an array where to store results to increase efficiency dramatically.

// Range we test
let FROM = 2
let UNTIL = 999_999

// Save sequence lengths in array for gains in performance
let ARRAY_SIZE = 1_000_000
let stepsArray = Array(count: ARRAY_SIZE, repeatedValue: -1)
stepsArray[1] = 0

Notice the nice

Continue reading →

Dealing with large numbers in Swift

Type inference is a handy feature in Swift that makes declaring most variables fun and easy. However, there are cases when you must explicitly define the type of the variable.

In this example, the starting point is having 4 big numbers, each consisting of 50 digits. Normally you might try to put them into a variable with type inference, like this:

var numbers =

Since the compiler is getting an integer like number fed to it, it tries to automatically use the type Int (Int64 on a 64-bit platform, Int32 on a 32-bit platform). This leads to a compile-time error of an overflow. Since the numbers are too big even for an UInt64 type, we must use floating point here

Continue reading →

Looping through an array of arrays with Swift

…and finding the greatest product horizontally, vertically or diagonally.

Finding out the largest product was fairly simple using Swift. The global enumerate function for looping through the arrays was an especially nice feature. It returns a tuple with the value and the index, see more in Apple’s Book “The Swift Programming Language” page 102. You can read the book from Apple’s iBooks for free.

Since this is my first Swift post I will post event the copyright header to show you what Xcode generates.

//  main.swift
//  Created by T. Urtti on 11.6.2014.
//  Copyright (c) 2014 T. Urtti. All rights reserved.

After this I create the array of int arrays. Now notice how I don’t declare anything explicitly to be an array nor do I explicitly define anything to be integer. This code fully utilizes the type inference that is a part of Swift. The compiler understands automatically

Continue reading →