2

I have a ton of identical SKSpriteNodes added to my scene, all with the same zPosition, layered on top of each other according to the order they are added to the scene. I need to be able to pick one SKSpriteNode, and add another at the same index (plus or minus one). I know I can use scene.insertChild(mySpriteNode, atIndex:index) but how do I get that index from a SKNode or SKSpriteNode? I have looked through the documentation, and online, there must be something I am missing, it seems like it would be so simple! My ideal solution: scene.insertChild(mySpriteNode, atIndex:someSpriteNode.siblingIndex) but I do not see a property on SKNode like siblingIndex, or maybe it would be called indexInParent.

I suppose I could keep count of every node I add to the scene, and subclass SKSpriteNode and set my own index, but I add nodes in a couple different places. This would be tedious and easily broken if I try and change my code later.

Maybe I could iterate through the entire children array property on the scene until I found the node === the node I am searching for, but this is very expensive and inefficient.

And using the zPosition is not ideal because I am using zPosition for a few other layers in the app, and the amount of SKSpriteNodes that can be added is unlimited, so eventually I would break through to the next layer.

Hopefully someone tells me I have simply overlooked this property somewhere! Any help is appreciated thanks!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
mogelbuster
  • 1,066
  • 9
  • 19
  • Afaik there is no order to an sknode or SKScene. You'd probably be right in subclassing and adding your own custom order/index – Scott Dec 28 '14 at 00:55
  • 1
    I don't think there's direct way to determine the index of a child. I think your only option is to iterate over the scene's children (as you mentioned). You can do this with `let index = find (self.children as [SKNode], sprite)`, which returns an optional `Int`. – 0x141E Dec 31 '14 at 08:54

0 Answers0