player doesnt move left to right. help

hello, i am trying to get a player on the screen to move from left to right but my code does not work. it only moves right when i tap the screen. This is my code

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if gameStarted == false{
           
            gameStarted = true
            completedLevel = false
           
            let movePlayerright = SKAction.moveByX(700, y:0, duration:3)
            let movePlayerleft = SKAction.moveByX(-700, y:0, duration:3)
            if gameStarted == true{
                Player.runAction(movePlayerright)
                Player.runAction(movePlayerleft)
                           }
           
        }

Thanks.

😁😀😝

It's not really clear what you're asking about. The node does move when you "tap", but your code "does not work". That seems contradictory.


Anyway, your code is wrong. The actions are started independently, so they run independently. That is, you're asking the Player node to move left and right at the same time. The two movements will therefore balance each other [almost] exactly, and there will be no visible movement.


If you want one action to move the player right first, then left afterwards, you need to combine the "right" and "left" actions into a SKAction sequence, then run the sequence.


Unrelated, but:


1. This is a Sprite Kit question, so should be in the Sprite Kit forum.


2. I strongly advise you to follow the standard Swift conventions for naming things, including word capitalization. There's a great document here:

swift.org/documentation/api-design-guidelines/

that explains the recommended conventions.


This is not about you being reprimanded by the thought police, but about getting better replies to your forum questions if we can easily read and understand your code fragments.

So like,

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if gameStarted == false{
           
            gameStarted = true
            completedLevel = false
           
            let movePlayerright = SKAction.moveByX(700, y:0, duration:3)
            let movePlayerleft = SKAction.moveByX(-700, y:0, duration:3)
            let movePlayerLR = SKAction.sequence([movePlayerright, movePlayerleft])
            Player.runAction(movePlayerLR)
                           }
           
        }
      

?

Thanks

That looks about right. Does it do what you want?

Kind of, There are 2 things that need to be fixed. When it starts moving left, I want the image to lip horizontally. And, I want it to keep on going from left to right in an endless loop.

You can flip a node horizontally by scaling by -1 in the x direction, and 1 in the y direction, and there's an action for that.


To keep it going in an endless loop, you need to repeat the sequence, and there's an action for that.


You really need to read, or at least skim, the Sprite Kit documentation, so that you have an idea of what it provides. Seeing what's possible should be enlightening and encouraging.

Also, check out the DemoBots sample code by Apple to see how to structure your code, so you can have logic implementend with GameplayKit and you can decouple your animation code from the controls code.

player doesnt move left to right. help
 
 
Q