touches始于physicsBody

touchesBegan with physhicsBody

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

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

例如:

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

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

我无法使用节点的矩阵位置进行触摸,因为图像与它重叠,所以它永远不会接触到矩阵方块。

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

来获得触摸
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

如果我理解你的问题。

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

我做的,在这个例子中我参考了Scene,但是你可以用其他的Sprite,好吧,我把我的屏幕分成4块,我得到了触摸点并在Scene中验证了位置。

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]
            }
        }
    }
}

希望对你有帮助..