首页 > 解决方案 > 为什么我在 touchesBegan 中没有节点?

问题描述

我在 MyBall 类中使用 touchesBegan,它本身就是一个 SKSpriteNode,所以当我点击 MyBall 时它会被调用,并且我确实越过了警戒线。

但是,当我打印touchNodes.count 时,我得到零,并且touchNodes 确实有一个原始值。

如果有人知道我在这里做错了什么......或失踪?

ps 我尝试从 GameScene 本身执行此操作,但从未收到 touchesBegan。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    
    let location = touch.location(in: self)
    let touchedNodes = self.nodes(at: location)
    print ("\(touchedNodes.count)")
    
    //combine next two lines
    for node in touchedNodes.reversed(){
        var thisBall = node as? MyBall
        if thisBall!.nodeType == "ball" {
            self.currentNode = node
        }
    }
}

标签: swiftsprite-kituikit

解决方案


当我继续搜索时,我意识到我在追逐自己的尾巴。寻找 touchesBegan 在错误的地方,以及拥有 GameScene 的isUserInteractionEnabled = false,以为我不会碰它。

最后,我找到了将所有 touchesBegan 都集中在一个地方的解决方案,GameScene。

所以要让所有 touchesBegan 发送到同一个场景,设置:GameScene'sisUserInteractionEnabled = true 和所有其他节点到isUserInteractionEnabled = false

这是 Apple Developer 的链接,它向我解释了这一切:

控制节点上的用户交互


推荐阅读