My app is a SpriteKit game with application state preservation and restoration. When application state is preserved, most of the nodes in my current SKScene are encoded.
When a node running an SKAction is encoded and decoded, the action will restart from the beginning. This appears to be standard SpriteKit behavior.
For me, this behavior is most noticeable for SKAction sequence. On decoding, the sequence restarts, no matter how many of its component actions have already completed. For instance, say the code to run the sequence looks like this:
[self runAction:[SKAction sequence:@[ [SKAction fadeOutWithDuration:1.0],
[SKAction fadeInWithDuration:1.0],
[SKAction waitForDuration:10.0],
[SKAction removeFromParent] ]]];
If application state is preserved during the 10-second wait, and then restored, the SKAction sequence will start again from the beginning, with a second visible fade-out-and-in.
It makes sense that SKAction sequence should show decoding behavior consistent with other actions. It would be useful, though, to make an exception, so that any actions already completed are not run again. How can I prevent a sequence restarting after decoding?
The
SKActionsequence can be decomposed into a number of subsequences such that, once a particular subsequence has finished, it will be no longer running, and so won't be restarted on decode.The Code
Make a lightweight, encodable object which can manage the sequence, breaking it into subsequences and remembering (on encode) what has already run. I've written an implementation in a library on GitHub. Here's the current state of the code in a gist.
And here's an example (using the same sequence as below):
The Concept
A first idea: Split the sequence into a few independent subsequences. As each subsequence completes, it will no longer be running, and so will not be encoded if the application is preserved. For instance, an original sequence like this:
could be split like this:
No matter when the node is encoded, the methods
doX,doY, anddoZwill only be run once.Depending on the animation, though, the duration of the waits might seem weird. For example, say the application is preserved after
doXanddoYhave run, during the 1-second delay beforedoZ. Then, upon restoration, the application won't rundoXordoYagain, but it will wait 11 seconds before runningdoZ.To avoid the perhaps-strange delays, split the sequence into a chain of dependent subsequences, each of which triggers the next one. For the example, the split might look like this:
With this implementation, if the sequence is preserved after
doXanddoYhave run, then, upon restoration, the delay beforedoZwill be only 1 second. Sure, it's a full second (even if it was half elapsed before encoding), but the result is fairly understandable: Whatever action in the sequence was in progress at the time of encoding will restart, but once it completes, it is done.Of course, making a bunch of methods like this is nasty. Instead, make a sequence-manager object, which, when triggered to do so, breaks the sequence into subsequences, and runs them in a stateful way.