我的代码在columnTime的持续时间内运行等待操作,然后运行一段代码。这导致块运行一次,然后等待时间,然后阻塞,然后等待时间等。
func startSpawning(){
print(columnTime)
let wait = SKAction.waitForDuration(columnTime)
let block = SKAction.runBlock({[unowned self] in self.spawnObstacle()})
let sequence = SKAction.sequence([wait, block])
runAction(SKAction.repeatActionForever(sequence), withKey: "spawning")
} //startSpawning
我想要以下内容:等待5秒,运行块代码,将列时间减少到4.95秒,运行等待4.95秒,运行块代码,将等待时间减少到4.90秒,运行等待4.90秒等。< / p>
我尝试了以下操作,但它一遍又一遍地运行每个动作,并且没有任何等待。我的输出充斥着来自等待和块代码块的打印语句。
func startSpawning(){
let wait = SKAction.runBlock({[unowned self] in self.waitFunc()})
let block = SKAction.runBlock({[unowned self] in self.spawnObstacle()})
let sequence = SKAction.sequence([wait, block])
runAction(SKAction.repeatActionForever(sequence), withKey: "spawning")
} //startSpawning
func waitFunc() -> SKAction{
print("running wait func")
return SKAction.waitForDuration(getColumnTime())
}
func getColumnTime() -> NSTimeInterval {
return columnTime
}
答案 0 :(得分:1)
一旦你将序列分配给序列,你就不明白变量是如何工作的了。序列将始终是创建时分配的任何内容。创建一个新实例不会解决这个问题,因为旧的实例仍然在序列中。每次运行序列时,您需要生成一组新的操作,而不是重复操作:
var columnTime : NSTimeInterval = 10
func startSpawning(){
let wait = SKAction.waitForDuration(columnTime)
let block = SKAction.runBlock()
{
[unowned self] in
columnTime -= 0.1
self.spawnObstacle()
self.startSpawning()
}
let sequence = SKAction.sequence([wait, block])
removeActionForKey("spawning")
runAction(sequence, withKey: "spawning")
}