我的应用程序崩溃是因为我的sprite-kit游戏中未完成的声音

时间:2017-03-18 12:48:13

标签: ios swift sprite-kit crash

无法理解为什么我的应用程序在播放声音时会崩溃。如果这里app没有声音会崩溃。 我有SKAction播放声音。我在static let课程中使用Actions预加载这首歌曲,然后拨打static func并发出SKAction声音。所以我有完整级别的声音。当它播放时,我点击主页按钮,他们回到应用程序。应用程序崩溃消息"线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xff8e9abc)"。有谁知道这里发生了什么?

这是我的结构Actions

public struct Actions {
    static let completionSound = SKAction.playSoundFileNamed("CompletionSound.mp3", waitForCompletion: false)

    static func playCompletionSound() -> SKAction {
        return completionSound
    }
}

在我的班级Grid中,我有条件是否已完成等级:

class Grid {
    var delegate: GridDelegate?
    private var completed: Bool = false
    ...

    ...
    private func completeLevel() {
        if completed {
            let waitAction = SKAction.wait(forDuration: 1.5)
            let blockAction = SKAction.run {
                let groupAction = self.settings.sound ? SKAction.group([Actions.playGridSwishSound(), Actions.removeGridAction()]) : Actions.removeGridAction()
                self.gridNode.run(groupAction, completion: {
                    self.gridNode.removeAllChildren()
                    self.delegate?.completionLevel()
                })
            }
            let seq = SKAction.sequence([waitAction, blockAction])
            run(groupAction)
        }
    }

我有一个节点,显示SKLabelNode" COMPLETED"文本。

class CompletionNode: SKNode {
    private let completionLabel = SKLabelNode(fontNamed: MainFont)

    init(settings: Settings) {
        self.settings = settings
        super.init()
        isUserInteractionEnabled = false

        completionLabel.text = Titles.CompletedTitle
        completionLabel.fontSize = 46.0
        completionLabel.fontColor = GoldenColor
        completionLabel.centering()
        completionLabel.position = CGPoint(x: 0.0, y: 0.0)
        completionLabel.xScale = 0.0
        completionLabel.yScale = 0.0
        completionLabel.alpha = 0.0

        addChild(completionLabel)
    }

    func animate(completion: @escaping () -> ()) {
        let waitAction = Actions.waitAction(duration: 0.2)
        let scaleAction = SKAction.scale(to: 1.0, duration: 0.4)
        scaleAction.timingMode = .easeInEaseOut
        let fadeInAction = Actions.fadeInAction(duration: 0.4)
        fadeInAction.timingMode = .easeIn
        let groupAction = SKAction.group([scaleAction, fadeInAction])

        let llBlock = SKAction.run {
            self.completionLabel.run(groupAction)
        }

        let seq = SKAction.sequence([waitAction, llBlock])
        run(seq, completion: completion)
    }
}

在我gameNode实例的grid中,我是最后一个的委托,我将completionNode添加到场景中:

class GameScene: SKNode {
    var gird: Grid?
    let hudNode = HUDNode()
    let completionNode = CompletionNode()
    var completionOn = false
    ...
    init() {
        super.init()
        grid = Grid()
        grid!.delegate = self
        grid!.position = CGPoint(x: 0.0, y: 0.0)
        addChild(grid!)

        hudNode.position = CGPoint(x: 0.0, y: MainSize.height * 0.44)
        hudNode.zPosition = 40.0
        hudNode.delegate = self
        addChild(hudNode)

        completionNode.alpha = 0.0
        completionNode.position = CGPoint(x: 0.0, y: 0.0)
        completionNode.zPosition = 60.0
        addChild(completionNode)
    }

    internal func completeLevel() {
        addCompletionScene()
    }

    private func addCompletionNode() {
        run(Actions.playCompletionSound())
        completionNode.alpha = 1.0
        completionNode.animate {
            self.completionOn = true
            self.hudNode.isUserInteractionEnabled = true
        }
    }
}

当我到达关卡结束时,按主页按钮,然后在发生崩溃后返回我的游戏。

1 个答案:

答案 0 :(得分:1)

当你的应用/游戏在前台和后台模式之间转换时,playSoundFileNamed似乎在保留音频会话方面存在问题。

根据您的实施情况,在应用程序在中断后恢复后,您将获得不可预测的行为,从静音播放到失败的加载声音资源,再到像您的情况一样严重崩溃。

以下是讨论类似问题的主题:SKAction playSoundFileNamed doesn't work after receiving two consecutive phone calls

我建议停止使用playSoundFileNamed操作并使用AVAudioPlayer来播放声音文件。它需要花费更多精力来配置它,但它已经存在了很长时间并且完美无缺。