随着游戏的进行,在 SKAction.waitForDuration() 中更改时间间隔
Change time interval in SKAction.waitForDuration() as game goes on
基本上这是重复动作序列中的动作之一。每次在序列中调用动作时,我都希望等待时间增加,所以我添加了一个计数器作为等待时间编号,并在序列中调用动作时递增。问题在于,变量仅在序列开始时递增,但在序列中再次发生动作时根本不会改变,等待时间在整个游戏过程中保持不变。我不知道如何在它被调用时改变它
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 操作在操作序列中创建、初始化和重用。每次都要重建一个动作。
基本上这是重复动作序列中的动作之一。每次在序列中调用动作时,我都希望等待时间增加,所以我添加了一个计数器作为等待时间编号,并在序列中调用动作时递增。问题在于,变量仅在序列开始时递增,但在序列中再次发生动作时根本不会改变,等待时间在整个游戏过程中保持不变。我不知道如何在它被调用时改变它
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 操作在操作序列中创建、初始化和重用。每次都要重建一个动作。