0
votes

In my Game I need to implement a 30 Second timer that starts when I tap on the screen so that if the player hasn't reached the objective before the timer reaches 0 its game over.

I can't figure out how to make the timer Count down to Zero after i touch the screen , it just stays at 30 sec . I tried implementing a NStimer but I'm aware you would have to add a PAUSE feature which i don't want ,plus the NStimer counts down before I touch the screen which i also don't want.

This is my code so far:

    var TimerNode: Int = 30
    var  TimerLabel = SKLabelNode(fontNamed: "STHeitJ-Medium")

    TimerLabel.text = "\(TimerNode)"
    TimerLabel.fontSize = 40
    TimerLabel.position.x = size.width / 2
    TimerLabel.position.y = size.height / 8.5
    TimerLabel.zPosition = 3.00
    TimerLabel.fontColor = UIColor.whiteColor()
    addChild(TimerLabel)
2
So, what is the problem? Please see : stackoverflow.com/help/how-to-askAnkur Aggarwal
I can't figure out how to make the timer Count down to Zero after i touch the screen , it just stays at 30 sec . I tried implementing a NStimer but I'm aware you would have to add a PAUSE feature which i don't want ,plus the NStimer counts down before I touch the screen which i also don't want..please help @AnkurAggarwalPaul Balestier
Moved OPs comment into the question to make it a question.Ali Beadle
OPs ?? i dont understand ??Paul Balestier
@PaulBalestier OP means "Original Poster".jacefarm

2 Answers

0
votes
self.addChild(myLabel)

You can implement a timer like this:

//Timer that updates Label:

myLabel.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
            TimerNode -= 1
            TimerLabel.text = "\(TimerNode)"

            if TimerNode <= 0 {

                 //Game Over code
            }
            },SKAction.wait(forDuration: 1)])))

Old:

//Run when clicked: 
var wait = SKAction.waitForDuration(30)
var run = SKAction.runBlock {
    //Game Over
}
self.runAction(SKAction.sequence([wait, run]))
0
votes
// ----- TIMER 2 Sec. ------
removeAction(forKey: "Timer") // Timer delete
var time = 20                 // start counter

self.run(SKAction.repeat(SKAction.sequence([SKAction.run {
    time -= 1
    print("\(time)")

     if time <= 0 {
         // start your code here!
     }

},SKAction.wait(forDuration: 0.1)]), count: time), withKey: "Timer")