0

I am making a game and have a bunch of vectors with each index corresponding to an SKSpriteNode within another vector. Can you extend the SKSpriteNode class to include the properties that I have stored in other vectors in corresponding indexes, then create objects of type ExtendedSKSpriteNode within the class declaration/implementation of an SKScene? I have tried with little success.

//Current Implementation
class OldScene: SKScene {
    var node1 = SKSpriteNode(imageNamed: "who cares")
    var node2 = SKSpriteNode(imageNamed: "doesn't matter")
    var node3 = SKSpriteNode(imageNamed: "this is instantiated")
    var nodeVector: [SKSpriteNode] = [node1, node2, node3]
    var timesWhenSomethingHappensToEachNode: [CGFloat] = [CGFloat(1.2), CGFloat(1.7), CGFloat(3.1)]
    var framesUntilThisOccurs: [Int] = [2, 30, 15]
}

//Desired implementation
extension SKSpriteNode {
    //timesWhenSomethingHappensToEachNode and framesUntilThisOccurs are made properties
    //of SKSpriteNode
    //probably happens in here
}

class NewScene: SKScene {
    //Can we instantiate those extended types here?
    var newNode1 = SKSpriteNode(imageNamed: "who cares")
    var newNode3 = SKSpriteNode(imageNamed: "this is instantiated")

    var nodeOneTime = newNode1.timesWhenSomethingHappensToEachNode
    var framesUntilThreeHappens = newNode3.framesUntilThisOccurs
}
Jack Frye
  • 583
  • 1
  • 7
  • 28
  • Why extend SKSpriteNode when you can just create an SKSpriteNode subclass? – Paulw11 Mar 02 '16 at 03:25
  • An extension can't have stored properties, but here's a [workaround](http://stackoverflow.com/questions/25426780/swift-extension-stored-properties-alternative). – Epsilon Mar 02 '16 at 19:30
  • good point, paul. polymorphism and conditional downcasting should make that pretty simple. thanks – Jack Frye Mar 03 '16 at 03:25

0 Answers0