Search code examples
iosswiftsprite-kituser-controlstouchesmoved

touchesMoved called without moving a finger


I'm trying to implement user control node with one ThumbStick and two buttons. I've create separate SKSpriteNode with control elements on it and override touch events of parent node to handle user touch.

The problem is that when I start touch screen, touchesMoved get called many times even if I don't move my finger.

Here is my touch events code:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

    for touch in touches {
        let touchPoint = touch.location(in: self)

        touchStatusLabel.text = String(format: "TOUCH BEGAN %@", arguments:[NSStringFromCGPoint(touchPoint)])

        if aButton.frame.contains(touchPoint) {
            NSLog("A BUTTON PRESSED")
            delegate?.controlInputNode(self, beganTouchButtonWithName: aButton.name!)
        }
        else if bButton.frame.contains(touchPoint) {
            NSLog("B BUTTON PRESSED")
            delegate?.controlInputNode(self, beganTouchButtonWithName: bButton.name!)
        }

        else if touchPoint.x < 0 && touchPoint.y < 0 {
            NSLog("THUMBSTICK PRESSED")
            leftThumbStickNode.touchesBegan([touch], with: event)
            leftThumbStickNode.position = pointByCheckingControlOffset(touchPoint)
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)

    for touch in touches {
        let touchPoint = touch.location(in: self)
        touchStatusLabel.text = String(format: "TOUCH MOVED %@", arguments:[NSStringFromCGPoint(touchPoint)])

        if !aButton.frame.contains(touchPoint) && !bButton.frame.contains(touchPoint) {
            if touchPoint.x < 0 && touchPoint.y < 0 {
                leftThumbStickNode.touchesMoved([touch], with: event)
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)

    for touch in touches {
        let touchPoint = touch.location(in: self)
        touchStatusLabel.text = String(format: "TOUCH ENDED %@", arguments:[NSStringFromCGPoint(touchPoint)])

        let node = atPoint(touchPoint)

        if node == aButton || node == bButton {
            delegate?.controlInputNode(self, endTouchButtonWithName: node.name!)
        }

        else {
            leftThumbStickNode.touchesEnded([touch], with: event)
        }
    }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)

    NSLog("TOUCH CANCELED")

    leftThumbStickNode.touchesCancelled(touches, with: event)
}

Solution

  • It appears that this problem occurs only during debugging so I assume that it is some debugger issue