【问题标题】:After upgrading to XCode7.2 SKAudioNode just playing 1 second - maybe issue with SKScene?升级到 XCode7.2 SKAudioNode 后只播放 1 秒 - SKScene 可能有问题?
【发布时间】:2015-12-13 10:26:03
【问题描述】:

升级到 XCode 7.2 后,我遇到一个问题,即 SKAudioNode 只播放了一秒钟,然后停止播放。我没有更改代码中的任何内容。

在我的 GameViewController 中,我将 MenuScene 称为:

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = MenuScene(size: view.bounds.size)
        let skView = view as! SKView

        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true

        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)

    }...

在我的 MenuScene 中,我这样称呼我的 GameScene:

func launchScene() {
        let gameView = view! as SKView
        let gameScene = GameScene(size: self.size)

        gameView.ignoresSiblingOrder = true

        let reveal = SKTransition.fadeWithDuration(0.5)

        gameView.presentScene(gameScene, transition:reveal)
    }

然后在我的 GameScene 中添加一个 SKAudioNode:

class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMoveToView(view: SKView) {
    ...
    let backgroundMusic = SKAudioNode(fileNamed: "main.mp3")
    backgroundMusic.autoplayLooped = true
    addChild(backgroundMusic)
    ...
    }
}

所以,问题是当我点击一个调用 launchScene() 函数的按钮时,背景音乐开始播放,但大约 1 秒后停止播放。

编辑:似乎背景音乐之前开始播放!到另一个场景的过渡开始了,当另一个场景(gameScene)“终于到了”(不知道如何描述它)时,音乐停止播放。我不知道为什么,因为我在“didMoveToView”函数的gameScene中添加了backgroundMusic。

我在这里做错了什么,因为它在 XCode 7.1 中完美运行?

【问题讨论】:

  • 只是一个疯狂的猜测,backgroundMusic.positional 有没有可能搞砸了?你确定backgroundMusic 不会很快打电话给removefromparentnode 吗?
  • 我评论了 .removeFromParentNode ,它就像描述的那样发生了。 .positional 也没有帮助。好像背景音乐之前开始播放了!到另一个场景的过渡开始了,当另一个场景(gameScene)“终于到了”(不知道如何描述它)时,音乐停止播放。我不知道为什么,因为我在“didMoveToView”函数的gameScene中添加了backgroundMusic。
  • 奇怪的事情,如果我将时间从 SKTransition.fadeWithDuration(0.5) 减少到 (0) 它可以工作。这是怎么回事?

标签: ios xcode swift sprite-kit tvos


【解决方案1】:

在我的带有场景过渡的项目中,即使在声音中添加 0.1 秒的延迟似乎也可以解决问题。比如

    runAction(SKAction.waitForDuration(0.1), completion: {
        self.backgroundMusic = SKAudioNode(fileNamed: "main.mp3")
        self.backgroundMusic.autoplayLooped = true
        self.addChild(self.backgroundMusic)
    })

【讨论】:

  • 我通过将 SKTransition.fadeWithDuration() 设置为 (0) “解决”了它,然后它就可以工作了。我猜这是 XCode 的问题?
猜你喜欢
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多