按特定类名搜索子级



在阅读了有关搜索子节点的 Apple 文档后,我明白我不仅可以按节点的名称进行搜索,还可以按其类进行搜索 - 通过在 withName 变量中使用 project_name.class_name。 我的项目名称是 color,我有一个类型为 SKShapeNode 的类名 BallNode。不幸的是,我无法使用此方法找到节点(如 apple doc 所写(.在我的项目中,球是在屏幕上随机创建的,每个球名称都是"BALL",当我按名称 (BALL( 搜索时,我可以找到节点,但如果我尝试使用类名它不起作用。这是我的代码:

    override func update(_ currentTime: TimeInterval) {
    self.enumerateChildNodes(withName: "color.BallNode") { node, _ in
        print("ball node found")
    }

有人知道我做错了什么吗?

来自

Apple的链接确实显示了正确的代码。我刚刚在测试项目中尝试了以下代码,它有效

    let ball = PushButton(upTexture: SKTexture(image:#imageLiteral(resourceName: "button")))
    ball.name = "ball1"
    addChild(ball)
    let ball2 = PushButton(upTexture: SKTexture(image:#imageLiteral(resourceName: "button")))
    ball2.name = "ball21"
    addChild(ball2)
    let ball3 = PushButton(upTexture: SKTexture(image:#imageLiteral(resourceName: "button")))
    ball3.name = "ball3"
    addChild(ball3)
    self.enumerateChildNodes(withName: "WordConstructor.PushButton") { node,_ in
            print("node.name (node.name)")
    }

我会检查您的项目名称是"颜色"而不是"颜色",您的项目中有多个目标吗?

你能显示你在哪里添加球到场景中的代码吗?

仅供参考,在您的更新语句中运行它将导致您的游戏尝试每秒找到这 60 次,这可能不是很理想

最新更新