Mamazur
Mamazur

Reputation: 179

SKSpriteNode chain

I would like to make "aware" SKSpriteNode aware that touch is outside the node.(for example changing color)

I would like to use tuochesMoved (out of node) if it is possible

I don't want to connect them in any other way. Those spries should be independend.

How to do that?

Thank you

Upvotes: 2

Views: 66

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

Obviously you can implement the touchesBegan method to handle touches events :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}

And the other events to handle touches with the current Swift 3 syntax are:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}

About this, if you don't want to subclass your SKSpriteNode to add useful properties, you can use the userData property to store information about other nodes or himself and also about the current context:

yourSprite.userData = NSMutableDictionary()
yourSprite.userData?.setValue(SKColor.blue, forKeyPath: "currentColor")
yourSprite.userData?.setValue(true, forKeyPath: "selected")

Upvotes: 1

Related Questions