Search code examples
swiftxcode6touches

swift touches - how to differentiate objects


I'm still not comfortable with the touches func.

I'm able to use it and it works ok, but I need to understand it to get further on with my project.

Could someone please help me to understand how I can determine which object have been touched? I really want to understand what's going on so in the future I can do i.e. a switch statement based on the type of object that was touched...

So here's the typical code I uses:

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touch : UITouch! = touches.anyObject() as UITouch

and here's what I want to be able to do:

switch touch {
case SKSpriteNode
   ....
case SKLabelNode
   ....
case SKNode
   ...

and so on...

Probably not correct, but this explain why I want to under how to be able to grab the information passed on to the touches func. Please point me in the right direction :-D


Solution

  • There is actually a section on this in the UITouch documentation:

    Working with touch events in SpriteKit.

    You can get the location with locationInNode and then figure out what is at that location with nodeAtPoint.