无限期减少间隔运行等待动作的功能

无限期减少间隔运行等待动作的功能

本文介绍了无限期减少间隔运行等待动作的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,该代码在columnTime的持续时间内运行一个等待动作,然后运行一个代码块.这样会导致块运行一次,然后等待时间,然后运行块,然后等待时间,等等.

I have code which runs a wait action for a duration of columnTime and then runs a block of code. This results in the block running once, then wait time, then block, then wait time, etc.

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秒,等等.

I want the following: wait for 5 seconds, run block code which decreases column time to 4.95 seconds, run wait for 4.95 seconds, run block code which decreases wait time to 4.90 seconds, run wait for 4.90 seconds, etc.

我尝试了以下操作,但是它一次又一次地运行每个动作,没有任何等待.我的输出充满了等待代码块和阻止代码块中的打印语句.

I tried the following but it runs each action over and over again and there isn't any waiting. My output floods with the print statements from both wait and block code blocks.

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
}

推荐答案

您不了解变量的工作原理,一旦分配了等待序列,就可以了.序列将始终是创建时分配的序列.创建一个新实例将无法解决此问题,因为旧实例仍在序列中.代替重复操作,您需要在每次执行该序列时都产生一组新的操作:

You are not understanding how variables work, once you assign wait to sequence, that is it. The sequence will always be whatever was assigned at the time of creation. Creating a new instance will not solve this problem, because the old one is still in the sequence. Instead of a repeat action, you need to spawn a new set of actions every time you run through the sequence:

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")
}

这篇关于无限期减少间隔运行等待动作的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:27