Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

Declare array of objects which you place in it, like

var ballArray: [DroneBall] = []
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading