Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

sprite kit - How to interrupt a SpriteKit sequence of actions and resume as if there was no interruption

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.

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

UPDATE

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.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..")
})

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...