Jason
Jason

Reputation: 445

How to update waitForDuration constant?

I have a flashing light that uses a SKAction sequence that hides and unhides a circle node. I want to be able to change the intervals at which it flashes based on two buttons. I declared a variable stdTime and I change it in the touchesBegan method but it's not working. What am I missing?

my didMoveToView:

let blink = SKAction.sequence([
        SKAction.waitForDuration(stdTime),
        SKAction.hide(),
        SKAction.waitForDuration(stdTime),
        SKAction.unhide()])

    let blinkForever = SKAction.repeatActionForever(blink)

    metronome!.runAction(blinkForever)

and my touchesBegan:

if upArrow!.containsPoint(location) {
            stdTime = stdTime + 0.1
            println("here: \(stdTime)")
        }

Upvotes: 2

Views: 67

Answers (1)

Skyler Lauren
Skyler Lauren

Reputation: 3812

waitForDuration takes in a NSTimeInterval and not a variable. So it takes whatever time that variable was set to at creation and does not refer back to the variable you used.

Depending on the result you are looking for this might help.

func startBlink(){
    let blink = SKAction.sequence([
        SKAction.waitForDuration(stdTime),
        SKAction.hide(),
        SKAction.waitForDuration(stdTime),
        SKAction.unhide()])

    let blinkForever = SKAction.repeatActionForever(blink)

    metronome!.removeActionForKey("blink")

    metronome!.runAction(blinkForever, withKey: "blink")
}


if upArrow!.containsPoint(location) {
    stdTime = stdTime + 0.1
    startBlink()
    println("here: \(stdTime)")
}

Another option without interrupting the sequence is to do something like this

func startBlink(){

    let blink = SKAction.sequence([
        SKAction.waitForDuration(stdTime),
        SKAction.hide(),
        SKAction.waitForDuration(stdTime),
        SKAction.unhide(),
        SKAction.runBlock( {
            self.startBlink()
        })])

    metronome?.runAction(blink, withKey: "blink")
}


if upArrow!.containsPoint(location) {
        stdTime = stdTime + 0.1
        println("here: \(stdTime)")
}

And recursively call the method on your own. That way each time it hits the end it will take the updated stdTime.

Upvotes: 2

Related Questions