【问题标题】:How come addChild() results in cannot convert value of type SKAudioNode to expected argument type UIViewController?为什么 addChild() 导致无法将 SKAudioNode 类型的值转换为预期的参数类型 UIViewController?
【发布时间】:2020-10-19 21:50:38
【问题描述】:

我从教程中复制了下面的代码。但是,会弹出关于 addChild 的错误。它说,“无法将 'SKAudioNode' 类型的值转换为预期的参数类型 'UIViewController'”。我该如何解决这个问题?

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)
    }
}

【问题讨论】:

    标签: swift xcode sprite-kit skaudionode


    【解决方案1】:

    我假设教程中的某个地方缺少一个场景,您可能应该将音乐添加到场景中。像这样的。

    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)
    
    

    检查this article作为参考

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2016-07-27
      • 2016-07-02
      相关资源
      最近更新 更多