Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit - touchesMoved not called often enough - fast movement

Im programming a SpriteKit based game in Swift. The SKScene contains a SKSpriteNode which contains SKShapeNodes. One of those shapes is the "player". The user should be able to drag and drop the player. I implemented different methods to get this working user touchesBegan, touchesMoved, touchesCanceled and touches Ended. Basically everything works fine, as long as the user drags the "player" at a normal speed. But if the user drags the "player" really quickly the touchesMoved method is not called often enough. I thought it maybe due to the fact that I use SKShapeNodes but based on the output (below) that does not seem to be the case.

The code:

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

let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
    self.startedTouchingPlayer()
}
super.touchesBegan(touches, withEvent: event)
print("TOUCHES BEGAN")

}

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

let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
    self.isTouchingPlayer(touchLocation)
    print("TOUCHES MOVED \(touchLocation)")
}
super.touchesMoved(touches, withEvent: event)

}

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

self.endedTouchingHelper()
super.touchesEnded(touches, withEvent: event)
print("TOUCHES ENDED")

}

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

print("TOUCHES CANCELED")
self.endedTouchingHelper()
super.touchesCancelled(touches, withEvent: event)

}

Print statements:

Normal Movement:

TOUCHES BEGAN
TOUCHES MOVED (-4.0, -241.499984741211)
TOUCHES MOVED (0.500015258789062, -239.0)
TOUCHES MOVED (4.0, -237.000015258789)
TOUCHES MOVED (11.0, -234.0)
TOUCHES MOVED (19.5, -229.499984741211)
TOUCHES MOVED (27.0000152587891, -225.499984741211)
TOUCHES MOVED (34.0, -221.499984741211)
TOUCHES MOVED (41.0, -217.499984741211)
TOUCHES MOVED (48.0, -213.499984741211)
TOUCHES MOVED (53.4999847412109, -210.0)
...
TOUCHES MOVED (77.5, -177.000015258789)
TOUCHES MOVED (77.5, -178.0)
TOUCHES ENDED

Fast movement:

TOUCHES BEGAN
TOUCHES MOVED (6.5, -277.5)
TOUCHES MOVED (3.0, -261.000030517578)
[*** at this point the finger is at a different position as the "player" ***] TOUCHES ENDED

Any kind of help is appreciated. Thanks.

like image 372
milanwittpohl Avatar asked Oct 16 '25 18:10

milanwittpohl


1 Answers

You haven't posted how you move your player, so i assume you just change your player position. I would recommend you to use SKActions to move your player.

func moveToPosition(oldPosition: CGPoint, newPosition: CGPoint) {

    let xDistance = fabs(oldPosition.x - newPosition.x)
    let yDistance = fabs(oldPosition.y - newPosition.y)
    let distance = sqrt(xDistance * xDistance + yDistance * yDistance)
    let sceneDiagonal = sqrt(world.frame.size.width * world.frame.size.width + world.frame.size.height * world.frame.size.height)

    self.runAction(SKAction.moveTo(newPosition, duration: Double(distance / sceneDiagonal / 2)))
}

world - SKNode of exactly same size as your SKScene

And use it like this: touchesMoved:

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

let touchLocation = touches.first!.locationInNode(self)
if(self.player.containsPoint(touchLocation)){
    moveToPosition(self.player.position, newPosition: touchLocation)
}
like image 193
Darvas Avatar answered Oct 18 '25 12:10

Darvas