如何中断SpriteKit动作序列并像没有中断一样继续执行 [英] How to interrupt a SpriteKit sequence of actions and resume as if there was no interruption

查看:135
本文介绍了如何中断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秒内可见,则中断了序列2秒钟(表示hideInterval动作加showSprite动作1秒钟,并且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天全站免登陆