Reputation:
i have a ball with a flat terrain, however i am trying to move the ball according to the user's touch
location, so when the user taps any where on the screen, i move my ball to the touch's X
position
My code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if (ball?.hasActions())! {
ball?.removeAllActions()
}
let moveBY = SKAction.moveBy(x: touch.location(in: view).x, y: 70, duration: 1)
self.ball?.run(moveBY)
}
}
however, when the user taps on the screen, the ball moves accordingly, but when it comes to a second tap, the ball won't move according to the touch's X
Upvotes: 1
Views: 42
Reputation: 1
You should try this:
func touchDown(atPoint pos : CGPoint) {
if ball.hasActions() {
ball.removeAllActions()
}
let moveBY = SKAction.moveBy(x: pos.x, y: 70, duration: 1)
self.ball.run(moveBY)
}
I changed the function
Upvotes: 0