xcode - 执行某项操作 "x"秒,然后执行其他操作

标签 xcode swift timer nstimer

所以我想知道一些事情。我有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)
}

还有我的接触开始:

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”游戏中玩家跳跃和滚动相同的功能。

最佳答案

您的意思是“我希望 hero 通过 hero.runAction() 执行另一个操作。

只需使用重载函数func runAction(_ action: SKAction,completion block: () -> Void)

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instm/SKNode/runAction:completion :

例如

hero.runAction(heroSlideAction) { 
    /* completion block goes here. Takes no input, returns nothing. */
    hero.runAction(someOtherAction)
}

关于xcode - 执行某项操作 "x"秒,然后执行其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897613/

相关文章:

ios - 在定点上旋转 UIImageView

ios - 在 ModuleName-Swift.h 中找不到 Xcode Pods 函数

ios - UINavigationBar 不会扩展或扩展到设备宽度

ios - 我将如何保存特定日期的文本字段?

python - 每秒运行多个函数,将结果写入文件

使用 C 在 Raspberry Pi 上设置时钟 gettime

c# - 有没有办法暂停所有 Threading.Timer 计时器?

ios - 使用 swift 1.2 之前版本构建框架

swift - 如何默认将某些 View Controller 设置为横向模式,将其他 View Controller 设置为纵向模式

swift - Braintree,跳过显示 Dropin 并直接选择 PayPal