在不同节点上进行多次SKAction的完成块

时间:2015-08-11 07:04:53

标签: ios swift sprite-kit skaction sknode

例如,我有一个包含子类SKSpriteNode的节点的数组。如何在阵列的所有节点上运行相同的操作,并且一次为所有操作设置一个完成块? 我在一个瓷砖游戏上工作,想要做一个连锁反应,当一些瓷砖被破坏时,在它们的破坏结束时再调用相同的函数来检查更多的链(递归)。
我现在的工作也很好,但链之间没有延迟。我怎么能做到这一点?检查下面的当前代码。
提前谢谢!

func checkTilesAndDestroy(inout tilesToCheck: [Tile]) {

    if tilesToCheck.count == 0 { return }

    let firstTileToCheck = tilesToCheck.removeAtIndex(0)

    let tilesToDestroy: [Tile] = self.getNeighbours(firstTileToCheck)

    if tilesToDestroy.count >= 3 {

        for tile in tilesToDestroy {

            /* some code here */

            if /* some code here */ {
                /* some code here */

                if /* some code here */ {
                    /* some code here */
                    tilesToCheck.append(anotherTile)
                } else {
                    /* some code here */
                }

                /* some code here */
            }

            tile.runAction(SKAction.scaleTo(0, duration: 0.1)) {
                tile.removeFromParent()
            }
        }
    }

    checkTilesAndDestroy(&tilesToCheck)
}  

我希望是这样的。

func checkTilesAndDestroy(inout tilesToCheck: [Tile]) {

    if tilesToCheck.count == 0 { return }

    let firstTileToCheck = tilesToCheck.removeAtIndex(0)

    let tilesToDestroy: [Tile] = self.getNeighbours(firstTileToCheck)

    if tilesToDestroy.count >= 3 {
        for tile in tilesToDestroy {
            /* some code here */
        }
    }

    self.runAction(SKAction.runBlock({
        for tile in tilesToDestroy {
            tile.runAction(SKAction.scaleTo(0, duration: 0.1)) {
                tile.removeFromParent()
            }
        }
    })) { [unowned self] in

        /* here al tiles actions are completed */
        self.checkTilesAndDestroy(&tilesToCheck)
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了一个完美的解决方法。

func checkTilesAndDestroy(inout tilesToCheck: [Tile]) {

    if tilesToCheck.count == 0 { return }

    let firstTileToCheck = tilesToCheck.removeAtIndex(0)

    let tilesToDestroy: [Tile] = self.getNeighbours(firstTileToCheck)

    if tilesToDestroy.count >= 3 {

        for tile in tilesToDestroy {

            /* some code here */

            tile.runAction(SKAction.scaleTo(0, duration: 0.1)) {
                tile.removeFromParent()
            }
        }
    }

    if tilesToCheck.count != 0 {
        self.runAction(SKAction.waitForDuration(0.1)) { [unowned self] in
            self.checkTilesAndDestroy(&tilesToCheck)
        }
    }
}