【问题标题】:Completion block for multiple SKAction on different nodes不同节点上多个 SKAction 的完成块
【发布时间】:2015-08-11 07:04:53
【问题描述】:

例如,我有一个包含 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)
    }
}

【问题讨论】:

    标签: ios swift sprite-kit skaction sknode


    【解决方案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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多