当达到一定分数时,让我的敌人产生更快的速度

时间:2015-05-11 23:12:53

标签: ios swift sprite-kit

func updateTimer() {
    if Score < 10 {
        runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addEnemy), SKAction.waitForDuration(1.0)])))
    }else if Score == 10 {
        runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addEnemy), SKAction.waitForDuration(0.5)])))
    }else if Score == 20 {
        runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addEnemy), SKAction.waitForDuration(0.1)])))
    }
}

当玩家达到某个分数时,我试图让生成更快但是这个代码有错误可以任何人帮我这个请 非常感谢那些回答我问题的人

我想让它像这样一个

func updateTimer() {
    if score < 3 {
    timerDots = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: "fireDot", userInfo: nil, repeats: true)

    // Dots get generated a bit faster than before after score reaches 3
    } else if score == 3 {
        timerDots?.invalidate()
        timerDots = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "fireDot", userInfo: nil, repeats: true)

    // Dots get generated even faster than before after score reaches 12
    } else if score == 12 {
        timerDots?.invalidate()
        timerDots = NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: "fireDot", userInfo: nil, repeats: true)
    }

}

1 个答案:

答案 0 :(得分:1)

使用实例变量和SKScene update:方法执行此操作更简单:

private var score: Int = 0
private var priorEnemyAddTime: CFTimeInterval = 0

private var nextEnemyAddTime: CFTimeInterval {
    var waitTime = score < 10 ? 1.0
        : score < 20 ? 0.5
        : 0.1
    return priorEnemyAddTime + waitTime
}

override func update(currentTime: CFTimeInterval) {
    if nextEnemyAddTime <= currentTime {
        priorEnemyAddTime = currentTime
        addEnemy()
    }
}

在每一帧上,你检查自上次添加敌人以来它是否足够长。如果是这样,则将当前时间保存为上次添加的时间,然后添加敌人。