Eri-Sklii
Eri-Sklii

Reputation: 589

Do something after a function is finish

So I am wondering about something. I have 3 functions:

func makeHeroRun(){
    let heroRunAction = SKAction.animateWithTextures([ heroRun2, heroRun3, heroRun4, heroRun3], timePerFrame: 0.2)
    let repeatedWalkAction = SKAction.repeatActionForever(heroRunAction)
    hero.runAction(repeatedWalkAction)
}
func makeHeroJump(){
    let heroJumpAction = SKAction.animateWithTextures([heroJump1, heroJump2, heroJump2], timePerFrame: 0.2)
    hero.runAction(heroJumpAction)
}
func makeHeroSlide(){
    let heroSlideAction = SKAction.animateWithTextures([heroSlide1, heroSlide2], timePerFrame: 0.1)
    hero.runAction(heroSlideAction)
}

And also my touchesBegan:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        var touch = touches.first as! UITouch
        var point = touch.locationInView(self.view)

        if point.x < size.width / 2 // Detect Left Side Screen Press
        {
            makeHeroSlide()
        }
        else
        if point.x > size.width / 2 // Detect Right Side Screen Press
        {
            makeHeroJump()
        }
    }

"Hero" is the player running in the game.

What i want is that when the "makeHeroSlide" is finish running, i want to repeat "makeHeroRun", so after the hero has been sliding, it should continue to run. And when the hero is jumping, it should stop running, and when the hero hits the ground, it should continue to run again. How can i do this? I want sort of the same functions that are in the "Line Runner" game in AppStore where the player Jumps and Roll.

Upvotes: 1

Views: 96

Answers (1)

Ahmed Al Hafoudh
Ahmed Al Hafoudh

Reputation: 8429

You can call runAction function with completion: as second parameter which you can use to specify block to execute after it finishes executing the action.

hero.runAction(heroSlideAction, completion: {() -> Void in
    // do something here
})

Upvotes: 1

Related Questions