2

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

Mamazur
  • 175
  • 2
  • 8
  • There's probably several ways to do this, but one that instantly comes to my mind is the didSet and willSet property observers of Swift. You could create a boolean variable inside the touched object that has a didSet or willSet upon itself (a function that activates when there's a change, a change that you'd do in touchesBegan method of your touched object) and this could tell the other sprite about the touch. But it would need to have its details, so this is a little messy... I'm not expert. – Confused Oct 26 '16 at 03:45
  • Sorry I wanted to get information that touchesMove outside of the node. Could you help me? Thank you – Mamazur Feb 20 '17 at 12:38

1 Answers1

1

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")
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133