Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give to sprite exact touch place?

My question is

When i touch black arrow areas i still touch whole picture, but i want only touch and have action when i touch red area only. I tried to give a name to spriteNode plane = childNode(withName: "play") but it take/touch all image frame not only alphaBody.

red and black areas

I did some searches on google but no positive results.

Many thanks.

like image 681
Chakiy Avatar asked Jan 20 '26 04:01

Chakiy


1 Answers

a really simple solution could be to put hit test areas on your plane object (I've left them slightly red for the example but you would make them transparent). I created my plane class in the editor and dragged it as a reference into my scene, but you could easily just program the hit zones from scratch as well.

enter image description here

Then in your plane class add them to the plane and detect touches within the plane class itself. It's not exact but not that much farther off then your alpha tracing was in your picture.

A nice bonus of this method is that you can isolate this touches to areas of the plane. Some uses for that could be...

  • touch left/right wing to turn that direction
  • touch body to refuel
  • touch wings to change guns

here is the code I used in my Plane class

class Plane: SKSpriteNode {

    private var background: SKSpriteNode!
    private var wingsHitTest: SKSpriteNode!
    private var bodyHitTest: SKSpriteNode!
    private var smallWingsHitTest: SKSpriteNode!

    init() {

        super.init(texture: nil, color: .clear, size: CGSize.zero)
    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)
        setup()
    }

    func setup() {

        isUserInteractionEnabled = true
        self.color = .clear

        if let background = self.childNode(withName: "//background") as? SKSpriteNode {
            self.background = background
        }

        if let wingsHitTest = self.childNode(withName: "//wingsHitTest") as? SKSpriteNode {
            self.wingsHitTest = wingsHitTest
        }

        if let bodyHitTest = self.childNode(withName: "//bodyHitTest") as? SKSpriteNode {
            self.bodyHitTest = bodyHitTest
        }

        if let smallWingsHitTest = self.childNode(withName: "//smallWingsHitTest") as? SKSpriteNode {
            self.smallWingsHitTest = smallWingsHitTest
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as UITouch? {

            let touchLocation = touch.location(in: self)

            if wingsHitTest.contains(touchLocation) {
                print("hit in the wings")
            }
            else if smallWingsHitTest.contains(touchLocation) {
                print("hit in the back wings")
            }
            else if bodyHitTest.contains(touchLocation) {
                print("hit in the body")
            }
        }
    }
}
like image 155
Ron Myschuk Avatar answered Jan 21 '26 18:01

Ron Myschuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!