I'm making a game where I move a sprite across the screen, but if I tap on the screen it will move to that location and I only want it to move if I hold my finger on the screen so the sprite will follow my finger and it wouldn't teleport through my objects
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchLocation = touch.location(in: self)
player.position.x = touchLocation.x
}
}
I tried this (the player is my sprite) and it works, when I move my finger the sprite will follow, but if I tap fx on the side of the screen it would teleport to that position and I don't want that to happen.
Try the following code:
touchesBegan - Makes sure that the touch location is somewhere within the player, by using
contains().touchesMoved - If the player is being dragged, move the player to the touch's location.
touchesEnded - When the touch has ended, dragging will stop.