Amani Elsaed
Amani Elsaed

Reputation: 1598

How to change the direction of a sprite after a user interacts with the scene?

I have an object of SKSpriteNode which has a sequence of actions to be run forever. The actions are simple to move to the most right then back to the most left.

When the user inputs some value in the app, I need to reverse the direction, i. e. if the sprite was moving to the right, I need to make it move to the left and vice versa.

How can I achieve this?

Upvotes: 2

Views: 73

Answers (2)

Cing
Cing

Reputation: 816

You should use:

SpriteNode.removeAllactions() // to stop the node

Maybe not correct spelling of function

Upvotes: 0

Darvas
Darvas

Reputation: 974

I would do it like this: (in swift)

func movament() {

    if isMovingRight == true {
        runAction(SKAction.sequence([
            SKAction.moveByX(10, y: 0, duration: 0.05),
            SKAction.performSelector("movament", onTarget: self),
            ]))
    } else {
        runAction(SKAction.sequence([
            SKAction.moveByX(-10, y: 0, duration: 0.05),
            SKAction.performSelector("movament", onTarget: self),
            ]))
    }
}

On user input change isMovingRight to oposite value

Was this helpfull?

Upvotes: 1

Related Questions