重复时,Skaction.playsoundfilenamed崩溃 - 精灵工具包

时间:2014-10-31 20:47:31

标签: ios swift sprite-kit skaction

我的精灵工具包SKAction.playSoundFileNamed有问题。 在实践中,经过一段时间它正确播放,应用程序崩溃说它不会加载。 该文件包含在捆绑导入中,项目文件存在且一切都已正确设置。

唯一的问题,经过一段时间我玩,我会崩溃说它找不到文件,或者至少无法加载。

我的问题是,每次声音SKAction.playSoundFileNamed有没有办法充电?

编辑 - 已解决

//init
var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
var sound2 = SKAction.playSoundFileNamed("sound2.mp3", waitForCompletion: false) 

//in the code call function when play sound:
playSound(sound)

...

func playSound(soundVariable : SKAction)
{
    runAction(soundVariable)   
}

实例化的预加载声音不再生成崩溃

2 个答案:

答案 0 :(得分:9)

预先声音变量

//init
var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
var sound2 = SKAction.playSoundFileNamed("sound2.mp3", waitForCompletion: false) 

//in the code call function when play sound:
playSound(sound)

...

func playSound(soundVariable : SKAction)
{
    runAction(soundVariable)   
}

答案 1 :(得分:-1)

我有一个类型为SKNode的小助手类来播放音频文件。 注意:必须将助手对象添加到SKScene层次结构中才能播放音频。

import UIKit

enum SFX_TYPE:Int
{
    case NEW_LEVEL = 0
    case BANG = 1
}

let SFXContainer:[SFX_TYPE:[SKAction]] = [
    SFX_TYPE.NEW_LEVEL : [SKAction.playSoundFileNamed("newlevel.m4a", waitForCompletion: true)],
    SFX_TYPE.BANG : [
        SKAction.playSoundFileNamed("explosion1.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion2.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion3.m4a", waitForCompletion: true),
        SKAction.playSoundFileNamed("explosion4.m4a", waitForCompletion: true)
    ]
]

class SabilandSound: SKNode {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    deinit
    {
        Helper.masterObserverRemove(self)
    }

    override init()
    {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("masterPlaySFX:"), name: NCNPlaySFX, object: nil)
    }

    func masterPlaySFX(n:NSNotification)
    {
        let st = SFX_TYPE(rawValue: n.userInfo![NCNPlaySFX] as! Int)!
        let c = SFXContainer[st]!
        let a = SFXContainer[st]![Helper.randomBetween(0, max: c.count, includeMax: false)]
        runAction(a)
    }

}