Drakalex
Drakalex

Reputation: 1538

Create a class which inherits from an array of SKSpriteNode in Swift

I'm trying to create a class in SpriteKit but I don't know if it's possible.

I know that :

class myClass: SKSpriteNode
{
    var myVar: Int = 0
    var mySecondVar: Int = 0
}

will create a class which inherits from the SKSpriteNode class.

Then, in order to create a 2 dimensionnal array from this class, I use this :

var array = [[myClass]]()

So I can add elements with

 var elements = [myClass]()

 elements.append(myClass(imageNamed: "image"))
 elements.append(myClass(imageNamed: "image"))

 array.append(elements)

So here, I can access myVar and mySecondVar using

 array[x][y].myVar
 array[x][y].mySecondVar

But what I want to do is that mySecondVar would be a variable from myClass, so here it's ok, but I want to access myVar using this sort of syntax :

 array[x].myVar
 array[x][y].mySecondVar

And I don't know how I could do this, I tried this :

 class myClass: [SKSpriteNode]

but it doesn't work. Could you try to help me ? Thanks.

Upvotes: 0

Views: 107

Answers (1)

DeFrenZ
DeFrenZ

Reputation: 2252

In that case, I suggest you do it differently altogether. In this way we represent the problem in a more specific and easy-to-use way:

class LaserBeam {
    var emitter: SKSpriteNode
    var receiver: SKSpriteNode
    var isActive: Bool
}
class LaserNode: SKSpriteNode {
    var temperature: CGFloat
}

var array: [LaserBeam] = ...

Upvotes: 1

Related Questions