如果像素是透明的,我如何制作一个不响应触摸的SKSpriteNode?

我正在用swift编写的sprite工具包为iOS创build游戏。 我有一堆SKSpriteNode在可以拖动的屏幕上。 他们是一个人,一个苹果,一本书等各种形状。问题是,人在苹果前面,但你可以通过人像(png)中的透明像素看到人后面的苹果。 当你去触摸苹果来移动它时,人会因为那些透明的像素而不是苹果而被选中。

如果像素是透明的,我如何制作一个不响应触摸的SKSpriteNode?

如果你想select一个被另一个精灵部分隐藏的精灵,你可以使用nodesAtPoint:CGPoint来获取用户触摸下的一个节点数组。 然后,您可以遍历数组中的节点来查找并select离触点最近的节点。 下面是一个在Swift中如何做的简单例子:

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch { let location = touch.locationInNode(self) var closest:CGFloat? let nodes = nodesAtPoint(location) for node in nodes as! [SKNode] { if let sprite = node as? SKSpriteNode { // Calculate the distance from the node to the touch let dx = location.x - node.position.x let dy = location.y - node.position.y let distance = dx*dx + dy*dy // Find the closest if closest == nil || distance < closest! { closest = distance selected = sprite } } } } } override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { if let sprite = selected { if let touch = touches.first as? UITouch { let location = touch.locationInNode(self) sprite.position = location } } } override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { selected = nil } 

下面是一个通过触摸上面的代码被移动的形状的剪辑:

在这里输入图像说明