How to interrupt SpriteKit and resume as if there were no interruptions

I would like to interrupt the sequence below for 2 seconds and return to where it would be if it were not interrupted in the first place.

For example, from the code below, if I interrupted the sequence for 2 seconds, making the sprite visible from 1.5 to 3.5 seconds in the sequence (which means that in 1 second the hideInterval action plus the showSprite action and 0.5 second action showInterval will be skipped), the interrupt will end for 2.5 seconds remaining for the showInterval action. How can I make a sprite run an action for the remaining 2.5 seconds and continue with a normal sequence at this point.

Note that this example is for clarity, although I would prefer a more general way to resume a sequence after an interrupt.

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

UPDATE

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

enter image description here

+5
source share
1 answer

To interrupt a sequence of actions like yours, you can simply pause your sprite with:

 sprite.isPaused = true 

and resume the action you can perform:

 sprite.isPaused = false 

To do some specific action after a while, you can simply do:

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

To answer your โ€œgeneral method to stop the sequence of actionsโ€, you cannot pause the sequence of actions, for example, using the parameters speed to 0, the sequence does not respond how it temporarily reacts to a normal action, because it is an action that performs a sequence of actions sequentially . An example or explanation of my words can be when you can have an action reporting sub-actions / code that calls other scenes / calls to other methods, so it is impossible to stop the sequence using conventional well-known methods.

+1
source

Source: https://habr.com/ru/post/1265037/


All Articles