Rae Tucker
Rae Tucker

Reputation: 523

How to make a sprite move left and right when screen is tapped?

so I have a game where my player needs to run left and right when told to. I have certain physics implemented so that it can jump when tapped, but it won't let me move forward or backwards on the ground. Think of a game like Super Mario where he can jump when told and also run forwards and back. Thanks(:

Upvotes: 0

Views: 1484

Answers (1)

MaxKargin
MaxKargin

Reputation: 1545

Use an SKAction to move or animate the player. The action you are looking for is moveByX:y:duration:. For example, if you have a sprite Node player you could say:

let moveAction = SKAction.moveByX(100, y:0, duration: 4)
player.runAction(moveAction)

the player sprite would then move by 100 points to right in 4 seconds. To be able to sense when the screen is tapped either use the touches: methods or implement a gesture recognizer. There are quite a few resources and questions on SO regarding these implementations.

Hope this helps, good luck.

Upvotes: 1

Related Questions