Published var in Class not available in array as a method
I am creating an array of SpriteNodes which each have an instance of another binded variable which is actually an instance of synthesiser code built with AudioKit as so...
var ballArray: [SKShapeNode] = []
var numberOfBalls = -1
class DroneBall: SKShapeNode, ObservableObject {
@Published var conductor = DynamicOscillatorConductor()
override init() {
super.init()
}
convenience init(width: CGFloat, point: CGPoint) {
self.init()
self.init(circleOfRadius: width/2)
fillColor = .red
strokeColor = .yellow
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I create each instance of this class in a function within a SpriteKit scene and add the ShapeNode to the scene.
func buildABall(location: CGPoint) {
let ball = DroneBall(width: 100, point: location)
ball.conductor.data.frequency = AUValue((location.x * 10))
ball.conductor.data.isPlaying = true
ball.conductor.start()
ballArray.append(ball)
numberOfBalls += 1
ballArray[numberOfBalls].position = location
addChild(ballArray[numberOfBalls])
}
The code is correctly creating the SpriteNode and will add to the scene each Sprite each one with the Synth running. However I am unable to access methods of the synth on the array such as
ballArray[numberOfBalls].conductor.data.frequency = 400
I get the error: Value of type 'SKShapeNode' has no member 'conductor'
So if it is there in the SpriteNode before being added to the array why is it not also in the array? I need to be able to access the values of 'conductor' elsewhere in the code so I can change the variables within.
Does the array also have to be an observable object in some way?
Declare array of objects which you place in it, like
var ballArray: [DroneBall] = []