2

I'm current developing a small game where balloons float up from the bottom of the screen and the player has to pop them before they reach the top.

Each balloon object contains a method to safely set up it's AVAudioPlayer's:

func setUpAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {

    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)

    var audioPlayer:AVAudioPlayer?

    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: url)
    }catch {
        print("BALLOON ERROR - AudioPlayer not avaliable.")
    }

    return audioPlayer
}

Then in each balloons init() method I run the following code:

if let popSound = self.setUpAudioPlayerWithFile("PopSound", type: "wav") {
        self.popSound = popSound
    }

Which works absolutely fine until a couple of minutes into the game. At this point I start receiving "BALLOON ERROR - AudioPlayer not available." in the console for every single balloon being spawned from then onwards indicating that my resource isn't being found?

At this time my SKEmitterNodes start to return nil as well. (2016-08-13 18:59:54.527 Stop & Pop[256:13785] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL)

Is there anything obvious I'm missing that could be causing these errors?

I hope I've provided enough information, thank you for reading.

M. Stolte
  • 21
  • 2

1 Answers1

0

No... this won't work. You have to instantiate the audioPlayer variable into the Class Level.

I tried to do this, instantiated the var audioPlayer on another ViewController at the Class Level.

Your best bet is to localize this method into your ViewController, and put the audioPlayer here:

class YourClassName : Type {
var audioPlayer : AVAudioPlayer?

yourMethodForCalling() {

}
Shashin Bhayani
  • 1,551
  • 3
  • 16
  • 37