【问题标题】:touchesBegan with physhicsBodytouchesBegins with physicsBody
【发布时间】:2015-09-14 23:01:09
【问题描述】:

我正在创建一个 2x2 矩阵,并将节点放入其中。当我在矩阵的某个位置选择一个节点时,他使用 colorize 操作将其颜色更改为红色。

问题是如果节点图像太大,即使我触摸矩阵的另一个正方形,他也会识别触摸。

例如:

[0-0][0-1]
[1-0][y]

我的节点在 Y 方块中(位置:1-1)。如果图像大于正方形的大小,即使我点击 1-0 正方形,他也会得到触摸。

我无法使用节点的矩阵位置获得触摸,因为图像与其重叠,因此它永远不会接触矩阵正方形。

无论如何我可以使用节点的 PhyshicsBody 而不是

let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

【问题讨论】:

    标签: swift sprite-kit touchesbegan


    【解决方案1】:

    如果我了解您的问题。

    我知道您需要绘制元素符合触摸位置。

    我做什么,在这个例子中我使用了场景作为参考,但是你可以用其他的 Sprite 来做,好吧,我把我的屏幕分成 4 块,我得到了接触点并验证了场景中的位置。

    class test:SKScene{
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
            if let touch = touches.first as? UITouch{
                let touchLocation = touch.locationInNode(self)
    
                if touchLocation.x < self.size.width/2 && touchLocation.y < self.size.height/2 {
                    //paint red [1,0]
                }else if touchLocation.x < self.size.width/2 && touchLocation.y > self.size.height/2 {
                    //paint red [0,0]
                }else if touchLocation.x > self.size.width/2 && touchLocation.y > self.size.height/2 {
                    //paint red [0,1]
                }else if touchLocation.x > self.size.width/2 && touchLocation.y < self.size.height/2 {
                    //paint red [1,1]
                }
            }
        }
    }
    

    希望对你有所帮助..

    【讨论】:

    • 这在某种程度上有所帮助,但我实际上是在做一个 5x9 矩阵。因此,做 45 个 else if 将是一项重大工作。我不明白为什么 nodeAtLocation 让我的 Sprite 超过了我的 PhyshicsBody。没有办法改变这种行为吗?
    • 好的,我明白了,但是你可以做其他方式,你可以创建2个节点,一个添加图像的节点,一个验证触摸的节点。或者您可以执行一些函数来获取 Array of Elements 中的 id,例如 API func getTouchId(ids:[Int], sizeOfScene:CGRect, touchPosition:CGPoint)-&gt;Int .. 我认为对您有所帮助
    • 谢谢!我认为这应该会有所帮助。但在我尝试不同的方法之前
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多