如何中断 SpriteKit 的一系列动作并像没有中断一样恢复 [英] How to interrupt a SpriteKit sequence of actions and resume as if there was no interruption

查看:22
本文介绍了如何中断 SpriteKit 的一系列动作并像没有中断一样恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想中断下面的序列 2 秒钟,然后恢复到它本来没有中断的位置.

I would like to interrupt the sequence below for 2 seconds and resume to where it would have been if it wasn't interrupted in the first place.

例如,从下面的代码中,如果我通过使精灵在序列中的 1.5 - 3.5 秒可见(意味着 hideInterval 动作加上 1 秒)来中断序列 2 秒showSprite 动作,而 showInterval 动作的 0.5 秒将被错过)中断将在 showInterval 动作剩余的 2.5 秒时结束.我怎样才能让精灵在剩下的 2.5 秒内运行一个动作并在那时继续正常的序列.

For example, from the code below, if I were interrupt the sequence for 2 seconds by making the sprite visible from 1.5 - 3.5 seconds into the sequence (meaning that 1 second the hideInterval action plus the showSprite action, and 0.5 seconds of the showInterval action would be missed) the interruption would end at 2.5 seconds remaining for the showInterval action. How can I get the sprite to run an action on the remaining 2.5 seconds and continue with the normal sequence at that point.

请注意,此示例是为了清楚起见,但我更喜欢使用更通用的方式在中断后恢复序列.

Please note that this example is for clarity, though I would prefer a more generic way to resume a sequence after interruption.

    let hideSprite = SKAction.fadeAlpha(to: 0, duration: 0.5)
    let showSprite = SKAction.fadeAlpha(to: 100, duration: 0.5)
    let hideInterval = SKAction.wait(forDuration: 2)
    let showInterval = SKAction.wait(forDuration: 3)
    let spriteSequence = SKAction.sequence([hideSprite, hideInterval, showSprite, showInterval])
    sprite.runAction(SKAction.repeatForever(spriteSequence))

更新

我认为下图将使我的问题更容易理解,并进一步解释我想通过中断序列实现的目标.

I think the following diagram will make my question more understandable and explain further what I want to achieve with the interruption of the sequence.

推荐答案

要中断像你这样的动作序列,你可以简单地暂停你的精灵:

To interrupt the action sequence like yours, you can simply put in pause your sprite with:

sprite.isPaused = true

并恢复您可以执行的操作:

and to resume the action you can do:

sprite.isPaused = false

要在一段时间后执行某些特定操作,您可以简单地进行:

To make some specific action after a time you can simply made:

sprite.run(SKAction.wait(forDuration: 2.5),completion:{
   print("after 2.5 sec do..")
})

要回答您的停止动作序列的通用方法",您不能暂停动作序列,例如将 speed 设置为 0,序列不会在时间上没有响应响应一个正常的动作,因为它是一个按顺序运行一系列动作的动作.关于我的话的一个例子或解释可能是当你可以有一个动作来报告子动作/调用其他场景/调用其他方法的代码,所以不可能用普通的已知方法停止序列.

To answer about your "generic method to stop an action sequence" you can't pause a sequence of actions for example by settings it's speed to 0, a sequence don't not responding as temporally respond a normal action because it's an action that runs a collection of actions sequentially. An example or explaination about my words could be when you can have an action that report sub-actions / code that calls other scenes / calls to other methods so it's impossible to stop the sequence with the ordinary knowed methods.

这篇关于如何中断 SpriteKit 的一系列动作并像没有中断一样恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆