如何在使用 enumerate ChildNode WithName 后只让一个 child 做某事?

How do I only make one child do something after using enumerateChildNodesWithName?

我使用 enumerateChildNodesWithName 命令为我的所有方块赋予物理特性,如下所示:

    func findBlock(theName:String){

        self.enumerateChildNodesWithName("//*"){
        node, stop in

            if node.name == theName{
                node.physicsBody?.categoryBitMask = physicsCategory.block
                node.physicsBody?.collisionBitMask = physicsCategory.laser
                node.physicsBody?.contactTestBitMask = physicsCategory.laser
            } 
        }
    }

现在我只希望其中一个方块在被激光击中时消失。但是,如果不让所有其他方块同时消失,我无法做到这一点。
我尝试使用 didBeginContact 中的这行代码来查找哪个块代表第一个主体并将其删除:

if firstBody.categoryBitMask == physicsCategory.block && secondBody.categoryBitMask == physicsCategory.laser{

        let block = SKSpriteNode()
        block.physicsBody = firstBody
        block.alpha = 1
        let byeBlock = SKAction.fadeOutWithDuration(0.5)
        let gone = SKAction.removeFromParent()
        let run = SKAction.sequence([byeBlock, gone])
        block.runAction(run)
        self.removeChildrenInArray([laser])

    }

但这最终也不起作用。 请帮忙!提前致谢!

我假设您会按应有的方式处理联系人,因此您提供的 if 阻止会正常工作。如果是这样,这就是您想要的:

if firstBody.categoryBitMask == physicsCategory.block && secondBody.categoryBitMask == physicsCategory.laser{
{
    //Downcast it to SKSpriteNode or whatever your block is
    if let block = firstBody.node as? SKSpriteNode {

        //We don't want to run removing action twice on the same block
        if block.actionForKey("removing") == nil {

            block.alpha = 1
            let fadeOut = SKAction.fadeOutWithDuration(0.5)
            let remove = SKAction.removeFromParent()
            block.runAction(SKAction.sequence([fadeOut, remove]), withKey: "removing")
        }

    }
    //Downcast it to SKSpriteNode or whatever your laser is
    if let laser = secondBody.node as? SKSpriteNode {
        self.removeChildrenInArray([laser])
    }

}