基本上这是重复 Action 序列中的 Action 之一。每次在序列中调用 Action 时,我都希望等待时间增加,所以我添加了一个计数器作为等待时间编号,并在序列中调用 Action 时递增。问题在于,变量仅在序列开始时递增,但在序列中再次发生 Action 时根本不会改变,等待时间在整个游戏过程中保持不变。我不知道如何在它被调用时改变它
var timeInterval = 0
//main method
override func didMoveToView(view: SKView) {
var scale = SKAction.scaleTo(5,duration: 2)
sprite = spriteSet(texture: SKTexture(imageNamed: "texture"), color: UIColor.brownColor(), size: CGSizeMake(100,100))
sprite.runAction(SKAction.repeatActionForever(SKAction.sequence([waitAction,scale,waitAction])))
}
//I want the time to increase each time this function is called in the sequence
func waitFunction() -> SKAction {
timeInterval++
return SKAction.waitForDuration(NSTimeInterval(timeInterval))
}
最佳答案
您可以使用递归的方式来完成您想要的(增加等待参数的持续时间):
class GameScene:SKScene {
var wait:NSTimeInterval = 0
override func didMoveToView(view: SKView) {
NSLog("Start") //Using NSLog just to print the current time
recursive()
}
func recursive(){
let recursive = SKAction.sequence([
SKAction.waitForDuration(++wait),
//Do your stuff here, eg. run scale, move...
SKAction.runBlock({[unowned self] in NSLog("Block executed"); self.recursive()})
])
runAction(recursive, withKey: "aKey")
}
}
您目前正在经历的是,wait 操作已在操作序列中创建、初始化和重用。您每次都必须重建一个 Action 。
关于swift - 随着游戏的进行,更改 SKAction.waitForDuration() 中的时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624292/