运行时修改重复的任务序列 [英] Modify a repeating task sequence while running
问题描述
我正在尝试做一个重复的任务,我可以改变它重复的延迟。以下是我正在使用的当前代码:
I am trying to make a repeating task where I can change the delay in which it repeats. Here is the current code I am using:
var actionwait = SKAction.waitForDuration(self.wait)
var actionrun = SKAction.runBlock({
self.count+=1
if (self.count % 2 == 0 && self.wait > 0.2){
self.wait -= 0.1
actionwait.duration = self.wait
}
for node in self.children{
if (node.isMemberOfClass(Dot)){
node.removeFromParent()
}
}
var radius = CGFloat(arc4random_uniform(100) + 30)
var newNode = Dot(circleOfRadius: radius)
var color = self.getRandomColor()
newNode.fillColor = color
newNode.strokeColor = color
newNode.yScale = 1.0
newNode.xScale = 2.0
newNode.userInteractionEnabled = true
newNode.setScene(self)
newNode.position = newNode.randomPos(self.view!)
self.addChild(newNode)
})
self.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
但是,似乎因为序列已经重复,改变延迟的持续时间不会影响任何事情。
However, it appears that because the sequence is already repeating, changing the duration of the delay does not effect anything.
推荐答案
当你创建 actionwait
时,它会将值保存在关闭并继续在你的 repeatActionForever中使用它们
when you create actionwait
it's going to save the values inside that closure and keep using them in your repeatActionForever
当你跟踪变化时,最好做那种事情在更新
方法中。在这种情况下使用操作可能不是最好的方法。
When you're tracking changes it's best to do that sort of thing in the update
method. Using actions in this case might not be the best approach.
我不确定你(对于你的游戏)何时需要检查更改。对于大多数使用更新
方法的方法是足够的
I'm not sure about when you (for your game) need to be checking for changes. For most things using the update
method is adequate
这是我在更新中实现计时器的一种方式
heres one way i implement a timer in update
// MY TIMER PROPERTIES IN MY CLASS
var missleTimer:NSTimeInterval = NSTimeInterval(2)
var missleInterval:NSTimeInterval = NSTimeInterval(2)
// IN MY UPDATE METHOD
self.missleTimer -= self.delta
if self.missleTimer <= 0 { // TIMER HIT ZERO, DO SOMETHING!
self.launchMissle() // LAUNCH MY MISSLE
self.missleTimer = self.missleInterval // OKAY LETS RESET THE TIMER
}
delta是此帧与最后一帧之间的时间差。它用于创建流体运动和跟踪时间。像这样的东西。这是spritekit项目中非常标准的。你通常需要使用delta projectwide
delta is the difference of time between this frame and the last frame. It's used create fluid motion, and track time. Things like that. This is pretty standard among spritekit projects. you usually need to use delta projectwide
声明这两个属性:
// time values
var delta:NSTimeInterval = NSTimeInterval(0)
var last_update_time:NSTimeInterval = NSTimeInterval(0)
这应该是更新方法的顶部看起来的方式
This should be how the top of your update method looks
func update(currentTime: NSTimeInterval) {
if self.last_update_time == 0.0
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.last_update_time = currentTime
这篇关于运行时修改重复的任务序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!