【问题标题】:Wait until property is true before continuing loop等到属性为真后再继续循环
【发布时间】:2015-11-20 07:16:39
【问题描述】:

尝试做一些我觉得应该很简单的事情,但到目前为止我尝试过的任何事情都没有奏效。我试图阻止这个循环继续,直到我的敌人的属性设置为 true。

我的敌人节点在行走状态下找出了通往玩家的路径。在计算出路径之前,我不想迭代到下一个敌人。我的敌人节点有一个 pathComplete 节点,我在 walk 状态下设置为 true。

这是在触摸时执行的。

       for node:AnyObject in self.children {


                if node is EnemyNode {

                    let enemy = node as! EnemyNode

                    enemy.destination = coordinate
                    enemy.stateMachine.enterState(WalkingState)


                }

            }

【问题讨论】:

  • 嗯...if cancel { break } ?

标签: swift sprite-kit gameplay-kit


【解决方案1】:

如果我理解你想要做什么,那么你应该使用递归而不是循环。

首先,您需要创建一些 enemyNodeArray,其中将包含您需要的对象。

然后你可以像这样制作两个函数:

func actionForObjectWithIndex(index: Int, completion block: (nextIndex: Int) -> Void) {
    guard index >= 0 && index < enemyNodeArray.count else {
        return
    }

    // do what you need with object in array like enemyNodeArray[index]...
    ...
    // Then call completion 
    block(nextIndex: index + 1)
}

func makeActionWithIndex(index: Int) {
    actionForObjectWithIndex(index, completion: {(nextIndex: Int) -> Void in
        self.makeActionWithIndex(nextIndex)
    })
}

然后像这样开始使用它:

if !enemyNodeArray.isEmpty {
    makeActionWithIndex(0)
}

该算法将获取数组中的每个对象,并对其执行某些操作,并且只有在完成上一项后才会移动到下一项。

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2017-09-07
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2014-05-23
    • 2021-10-15
    • 2017-08-19
    相关资源
    最近更新 更多