Sprite Kit枚举要删除的节点失败

时间:2023-01-23 13:02:56

I am trying to enumerate nodes using their name, check their y position and if the value is lower than some number, run certain function on them. The nodes I am enumerating are part of other node, called blocks node. I set up my game this way because it is more convenient to apply action on the whole blocks parent node instead of on every block. This is my code for enumerating :

我试图使用他们的名字枚举节点,检查他们的y位置,如果值低于某个数字,则对它们运行某些功能。我枚举的节点是其他节点的一部分,称为块节点。我以这种方式设置游戏,因为在整个块父节点上而不是在每个块上应用操作更方便。这是我枚举的代码:

func chechForPassing(){
    var passedBlocks: [SKSpriteNode] = []
    enumerateChildNodesWithName("1") { node, _ in
        let node = node as! SKSpriteNode
        if (node.position.y <= self.frame.size.height*0.3) {
            passedBlocks.append(node)
            println("1")
        }
        for node in passedBlocks {
            self.fadeEnRemove(node)
        }

    }

where fadeEnRemove is just a function I wrote. Print is used for debuging purposes. This is how I set up my blocks:

其中fadeEnRemove只是我写的一个函数。打印用于除垢目的。这是我设置块的方式:

    func block(x: CGFloat, y: CGFloat) {

    var block = SKSpriteNode(imageNamed: "obsticle")
    kratkowski.size = CGSizeMake(block.frame.size.width*scaleFactor, block.frame.size.height*scaleFactor)
    block.position = CGPointMake(x, y)
    block.name = "1"
    blocksNode!.addChild(block)

}

And block node is SKNode. Anyone knows how to solve this little problem?

块节点是SKNode。谁知道如何解决这个小问题?

Thanks!

1 个解决方案

#1


If I'm following correctly, the enumeration block is never being called. This is probably because either the child nodes names aren't "1", or you aren't calling enumerateChildNodesWithName on the correct node.

如果我正确地关注,则永远不会调用枚举块。这可能是因为子节点名称不是“1”,或者您没有在正确的节点上调用enumerateChildNodesWithName。

Right now you're calling enumerateChildNodesWithName on self. Are these nodes added to self or are they added to another node? If they're on another node then call enumerateChildNodesWithName on that node instead.

现在你在self上调用enumerateChildNodesWithName。这些节点是添加到自身还是添加到另一个节点?如果它们在另一个节点上,则在该节点上调用enumerateChildNodesWithName。

#1


If I'm following correctly, the enumeration block is never being called. This is probably because either the child nodes names aren't "1", or you aren't calling enumerateChildNodesWithName on the correct node.

如果我正确地关注,则永远不会调用枚举块。这可能是因为子节点名称不是“1”,或者您没有在正确的节点上调用enumerateChildNodesWithName。

Right now you're calling enumerateChildNodesWithName on self. Are these nodes added to self or are they added to another node? If they're on another node then call enumerateChildNodesWithName on that node instead.

现在你在self上调用enumerateChildNodesWithName。这些节点是添加到自身还是添加到另一个节点?如果它们在另一个节点上,则在该节点上调用enumerateChildNodesWithName。