如何中断 SpriteKit 动作序列并像没有中断一样恢复 [英] How to interrupt a SpriteKit sequence of actions and resume as if there was no interruption
问题描述
我想中断下面的序列 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 秒可见而中断序列 2 秒(这意味着 1 秒 hideInterval
动作加上showSprite
动作,并且将错过 0.5 秒的 showInterval
动作)中断将在 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屋!