Search code examples
swiftbuttonsprite-kittouchmulti-touch

SpriteKit Scale on tap


im trying to create a "button touch" effect for one of my sprites, it works well but then I tap with 2 or more fingers at the same time, i get really weird results, here is my code:

let buttonPressAction = SKAction.scaleBy(0.8, duration: 0)

var button = SKNode()


override func didMoveToView(view: SKView) {

    //assign sprite to node

    button = self.childNodeWithName("button") as! SKSpriteNode!

  }

for touch: AnyObject in touches {

    let location = touch.locationInNode(self)

    if button.containsPoint(location)  {

        button.runAction(buttonPressAction)

    }
 }

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    button.runAction(buttonPressAction.reversedAction())
}

Solution

  • Try changing the SK scale action from

    ...scaleBy
    

    to

    ...scaleTo
    

    to ensure it will always scale to the same size. With scaleBy it will scale it by 0.8, not to 0.8. That most likely causes the weird results on multiple touches because you are scaling by 0.8 for each finger/tap.

    I never used reverseAction before so I am not sure if that might cause issues. If it does just reset the button by scaling it back to 1

    ...scaleTo(1, duration: 0)
    

    As as side note you can just say

    for touch in touches 
    

    instead of

    for touch: AnyObject in touches