Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a SpriteKit timer?

I am currently trying to implement a timer for my sprite kit game, but I don't get it working. The initial value of the timer always remains the same.

I am assuming I need to update the label somehow/somewhere, but I don't know HOW and WHERE :? I don't get the point. Any ideas?

Here is my code within my GameScene Class

    let levelTimerLabel = SKLabelNode(fontNamed: "Chalkduster")
var levelTimerValue: Int  = 500

var levelTimer = NSTimer()

func startLevelTimer() {

    levelTimerLabel.fontColor = SKColor.blackColor()
    levelTimerLabel.fontSize = 40
    levelTimerLabel.position = CGPoint(x: size.width/2, y: size.height/2 + 350)
    addChild(levelTimerLabel)

    levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("levelCountdown"), userInfo: nil, repeats: true)

    levelTimerLabel.text = String(levelTimerValue)

}

func levelCountdown(){

    levelTimerValue--

}
like image 710
boehmatron Avatar asked Feb 01 '16 20:02

boehmatron


2 Answers

I would stick to SKActions for these kind of tasks in SpriteKit due to fact that NSTimer is not affected by scene's, or view's paused state, so it might lead you into troubles. Or at least, it will require from you to implement a pause feature in order to pause your timers in certain situations, like when user pause the scene, or receive a phone call etc. Read more here about SKAction vs NSTimer vs GCD for time related actions in SpriteKit.

import SpriteKit

class GameScene: SKScene {

var levelTimerLabel = SKLabelNode(fontNamed: "ArialMT")

//Immediately after leveTimerValue variable is set, update label's text
var levelTimerValue: Int = 500 {
    didSet {
        levelTimerLabel.text = "Time left: \(levelTimerValue)"
    }
}

override func didMoveToView(view: SKView) {



levelTimerLabel.fontColor = SKColor.blackColor()
levelTimerLabel.fontSize = 40
levelTimerLabel.position = CGPoint(x: size.width/2, y: size.height/2 + 350)
levelTimerLabel.text = "Time left: \(levelTimerValue)"
addChild(levelTimerLabel)

    let wait = SKAction.waitForDuration(0.5) //change countdown speed here
    let block = SKAction.runBlock({
        [unowned self] in

        if self.levelTimerValue > 0{
            self.levelTimerValue--
        }else{
            self.removeActionForKey("countdown")
        }
    })
    let sequence = SKAction.sequence([wait,block])

    runAction(SKAction.repeatActionForever(sequence), withKey: "countdown")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    //Stop the countdown action

    if actionForKey("countdown") != nil {removeActionForKey("countdown")}
}
}
like image 138
Whirlwind Avatar answered Nov 16 '22 00:11

Whirlwind


Sounds like you need to update the label everytime levelTimerValue is changed. The easiest way would be something like this.

func levelCountdown(){
    levelTimerValue--
    levelTimerLabel.text = String(levelTimerValue)
}
like image 34
Craig Siemens Avatar answered Nov 15 '22 23:11

Craig Siemens