ios - Swift Spritekit count touches -
i'm trying have several actions on 1 node.
so example on first touch on node, first action should run. on second touch: second action should run.
below not working example of code touches.count.
override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { touch: anyobject in touches { let location = touch.locationinnode(self) let node = self.nodeatpoint(location) if node == mynode { if touches.count == 1 { action1() } if touches.count == 2 { action2() } if touches.count == 3 { action3() } } } }
you need member variable tracks number of touches since app started up. method touches.count isn't cumulative.
var cumulativenumberoftouches = 0 override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { touch: anyobject in touches { let location = touch.locationinnode(self) let node = self.nodeatpoint(location) if node == mynode { cumulativenumberoftouches += 1 switch cumulativenumberoftouches { case 1: action1() case 2: action2() case 3: action3() default: /* or nothing or whatever */ println("\(cumulativenumberoftouches) touches") } } } }
Comments
Post a Comment