SCNAudioSource不会使用SCNNode

时间:2018-04-14 04:40:45

标签: ios swift xcode scenekit scnnode

我每次点按屏幕时都会尝试播放声音。这是我的代码

全局:

var shapeNode: SCNNode!
var SoundAction: SCNAction!

viewDidLoad中:

    let audioSource = SCNAudioSource(named: "launch.mp3")!
    audioSource.isPositional = true
    audioSource.volume = 1.0
    SoundAction = SCNAction.playAudio(audioSource, waitForCompletion: false)

@objc func sceneTapped(识别器:UITapGestureRecognizer)

    shapeNode = SCNNode(geometry: myshape)

    self.myscene?.rootNode.addChildNode(shapeNode)
    shapeNode.runAction(SoundAction)

当我触摸屏幕时,声音不会播放......有人请帮忙

1 个答案:

答案 0 :(得分:0)

我生成了一个默认游戏项目,然后输入你的代码。起初,我收到了相同的结果。但是,当我拍摄let scene = SCNScene(名为:" art.scanassets / ship.scn")时!并使场景成为一个类变量,声音播放。或者,如果您在我们加载期间添加SoundNode而不是在点击时添加SoundNode,则声音也会播放。

class GameViewController: UIViewController {

    var SoundAction = SCNAction()
    var SoundNode = SCNNode()
    var scene = SCNScene()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Change this
        scene = SCNScene(named: "art.scnassets/ship.scn")!

        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

        let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
        ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))

        let audioSource = SCNAudioSource(named: "MenuWinner.caf")!
        audioSource.volume = 1.0
        SoundAction = SCNAction.playAudio(audioSource, waitForCompletion: false)

        let scnView = self.view as! SCNView
        scnView.scene = scene
        scnView.allowsCameraControl = true
        scnView.showsStatistics = true
        scnView.backgroundColor = UIColor.black

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        scnView.addGestureRecognizer(tapGesture)
    }

    @objc
    func handleTap(_ gestureRecognize: UIGestureRecognizer) {
        scene.rootNode.addChildNode(SoundNode)
        SoundNode.runAction(SoundAction)
    }