2
votes

I am creating a game for iOS using sprite kit written in swift. I have a bunch of SKSpriteNode on a screen that can be dragged around. They are various shapes such as a person, an apple, a book etc. The problem is, say the person is in front of the apple but you can see the apple behind the person through the transparent pixels in the persons image (png). When you go to touch the apple to move it, the person gets selected because of those transparent pixels instead of the apples.

How do I make a SKSpriteNode that does not respond to touch if it's pixels are transparent?

1
What do you mean by "the transparent pixels"? Can you show a screenshot?WangYudong
I can't show an image from the project because the owners will not allow it, but I will try to describe it more clearly. The image is of a person who has his arms straight out to the side forming a t with his body. SpriteKit sees this as a rectangle with a height going the top of the head to the bottom of his feet and a width going from finger tip to finger tip. Even though the image above and below his arms are mostly just empty space. That empty space under his arm will not allow the user to touch a object that is visible.user3491793
What you are asking requires a pixel perfect algorithm. One way to do is define a CGPath along the character's outline and detect if the touch point lies inside the CGPath.johnny peter
I will try this tonight. Thank you for leaving a comment.user3491793

1 Answers

2
votes

If you want to select a sprite that is partially hidden by another sprite, you can use nodesAtPoint:CGPoint to obtain an array of nodes that are under the user's touch. You can then iterate over the nodes in the array to find and select the node closest to the touch point. Here's a quick example of how to do that in 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
}

Here's a clip of shapes being moved by touch with the above code:

enter image description here