1

I am making a game using SpriteKit and swift. Some of my nodes have runBlock actions which call functions:

SKAction.runBlock(someFunction)

When I encode and decode my scene using NSCoder I get this message printed in the console

SKAction: Run block actions can not be properly encoded,
Objective-C blocks do not support NSCoding.

Swift does not support selector SKActions so how can I solve this problem.

TL;DR: How can I make an SKAction which calls a function and is encodable?

  • is there another type of SKAction that does this?

  • should I crete a customer SKAction subclass?

Any help is much appreciated :)

2 Answers2

1

You can't, and there's no real alternative. Blocks can't be encoded.

What you can do is to try and make your code work without block actions, if this is in any way possible.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks, I guess I could add the actions to the nodes again in `init(coder:)`. –  Jan 07 '15 at 16:56
0

SKAction performSelector can get the job done, although it's a bit messy.

The main annoyances are:

  1. passing arguments to the selector; and

  2. replacing customActionWithDuration:runBlock.

To address both of these problems, I wrap the selector call in a lightweight object which gets encoded along with its corresponding SKAction.

My results are a work in progress. I'm documenting the work on a StackOverflow question here (I would have used yours if I had seen it, sorry!).

My code (implementing the lightweight wrapper objects) is here.

Karl Voskuil
  • 750
  • 6
  • 19