DNC
DNC

Reputation: 443

How to detect if SKAction is running

This is my SKAction:

 naarRechts  = SKAction.moveToX(positionX , duration: 0.22)
player.runAction(naarRechts)

In that duration of 0.22 sec I do NOT want this action to run:

    if CGRectIntersectsRect(player.frame, car.frame){
        player.position.x = car.position.x
    }

What is the magic line of code where I can detect if my first SKAction is running, or to detect wether the player is in movement or not..

Upvotes: 2

Views: 838

Answers (1)

Kalzem
Kalzem

Reputation: 7501

You can create a class variable var playerIsInAction = false then set it to true just after you run the action. Also change the method so that you can write a completion code (after the action ends) where you set the bool back to false.

It should be something like this :

playerIsInAction = true
player.runAction(naarRechts, completion: {() in 
    playerIsInAction = false
})

And you check for the bool

if CGRectIntersectsRect(player.frame, car.frame) && playerIsInAction == false {
    //Code
}

Upvotes: 2

Related Questions