ddd
ddd

Reputation: 151

How come addChild() results in cannot convert value of type SKAudioNode to expected argument type UIViewController?

I copied the code below from a tutorial. However, an error about addChild pops up. It says, "Cannot convert value of type 'SKAudioNode' to expected argument type 'UIViewController'". How can I fix this?

import AVFoundation

import SpriteKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func didMove(to view: SKView) {
        let music = SKAudioNode(fileNamed: "3D_Beep.mp3")
        addChild(music)

        music.isPositional = true
        music.position = CGPoint(x: -1024, y: 0)

        let moveForward = SKAction.moveTo(x: 1024, duration: 2)
        let moveBack = SKAction.moveTo(x: -1024, duration: 2)
        let sequence = SKAction.sequence([moveForward, moveBack])
        let repeatForever = SKAction.repeatForever(sequence)

        music.run(repeatForever)
    }
}

Upvotes: 0

Views: 111

Answers (1)

Igor
Igor

Reputation: 1332

I assume somewhere in the tutorial is missing a scene, and you should probably add the music to the scene. Something like this.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.scene  = SKSCene(filenamed: "asdf")
}
...

let music = SKAudioNode(fileNamed: "3D_Beep.mp3")
self.scene?.addChild(music)

Check this article as a reference

Upvotes: 0

Related Questions