SKTimer 0.2.1

SKTimer 0.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2016
SPMSupports SPM

Maintained by Kyle Goslan.



SKTimer 0.2.1

SKTimer

Requirements

Designed to be used in a SpriteKit project.

Usage

Create an instance of an SKTimer object

let timer = SKTimer()

Call the start(_:) method on your new timer passing in the current time from the scene. This is the currentTime parameter from your scenes update method. Usually you’ll want to create a property in your scene to hold this value so you’ll have access to it from outside your update method:

timer.start(currentTime)

In your scenes update method make sure you call the timers update(_:completion:) method passing in the current time. This is how the timer calculates its count:

timer.update(currentTime, timeUp: nil)

You can get the current time of the timer from it time property:

print(timer.time)

Example Scene

Here is very simple example scene that starts an SKTimer on a touch:

import SpriteKit
import SKTimer

class GameScene: SKScene {

    var currentTime = 0.0
    var timer = SKTimer()

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        timer.start(currentTime)
    }

    override func update(currentTime: CFTimeInterval) {
        self.currentTime = currentTime

        if timer.on {
            print(timer.time)
            timer.update(currentTime, timeUp: nil)
        }
    }
}

Time Limits

You can set optional time limits for your timer and receive a callback when that limit is reached. Create a timer with a limit of 10 seconds like this:

var timer = SKTimer(limit: 10)

Multiplier

The default of the multiplier property is 1.0. You can increase or decrease this to speed up or slowdown the speed of the counter. E.g to create a timer that is twice as fast as real time:

var timer = SKTimer(multiplier: 2.0)

Installation

SKTimer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SKTimer"

Author

KyleGoslan

License

SKTimer is available under the MIT license. See the LICENSE file for more info.