【问题标题】:Do something after a function is finish函数完成后做某事
【发布时间】:2015-08-09 14:56:09
【问题描述】:

所以我想知道一些事情。我有 3 个功能:

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)
}

还有我的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()
        }
    }

“英雄”是在游戏中奔跑的玩家。

我想要的是当“makeHeroSlide”完成运行时,我想重复“makeHeroRun”,所以在英雄滑动后,它应该继续运行。而当英雄在跳跃时,它应该停止奔跑,当英雄落地时,它应该再次继续奔跑。我怎样才能做到这一点?我想要与 AppStore 中的“Line Runner”游戏中玩家跳跃和滚动的相同功能。

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    您可以使用completion: 作为第二个参数调用runAction 函数,您可以使用它来指定块在执行完操作后执行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多