DropCharge SKAudioNode crash page 444

In the book’s 444th page the code is crashing. I followed the code with break point and the variable backgroundMusic is always nil. Maybe this crash is happening after new update of Xcode. I tried to run the final template of the chapter 17 and it also crashing in the same line

func playBackgroundMusic(name: String) {
var delay = 0.0
if backgroundMusic != nil {
backgroundMusic.removeFromParent()
} else {
delay = 0.1 }
runAction(SKAction.waitForDuration(delay)) {
self.backgroundMusic = SKAudioNode(fileNamed: name)
self.backgroundMusic.autoplayLooped = true//Crashing line
self.addChild(self.backgroundMusic)
} }

Error is:(backgroundMusic is nil cannot find the file)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

Can anyone find the reason please. Also yes I have the sounds file in the directory

I see the same here, it worked fine before the latest XCode update, and it will also work again if i remove the background music

The reason is that in Xcode 7.3 the code statement that says:

runAction(SKAction.waitForDuration(delay)) {

is not supported and needs to be reinitialized before using it .
so it is easier to just change the func play background Music to
the following.

func playBackgroundMusic(name: String) {

    if backgroundMusic != nil {
        backgroundMusic.removeFromParent()
        if bgMusicAlarm != nil {
            bgMusicAlarm.removeFromParent()
        } else {
            bgMusicAlarm = SKAudioNode(fileNamed: "alarm.wav")
            bgMusicAlarm.autoplayLooped = true
            addChild(bgMusicAlarm)
        }
    }
    runAction(SKAction.playSoundFileNamed("SpaceGame.caf", waitForCompletion: false))
    self.backgroundMusic = SKAudioNode(fileNamed: name)
    }

and then put in this statement :
var backgroundMusic: SKAudioNode!

under properties right above the line:
var bgMusicAlarm: SKAudioNode!

and drop charge should work exactly as before in Xcode version 7.2

Thanks for your comment but still not working. As I followed the code with debugging breakpoint the backgroundMusic variable is still nil. The code is not giving any error but there is no music playing either. Also shouldn’t we need to add backgroundMusic as child to scene but since it is nil this will give error of course. I hope ray and their team releases an update. Any help is appreciated.

yes, in the code I sent two lines were missing:
they go right after self.background = SKAudioNode(fileNamed: name)

func playBackgroundMusic(name: String) {

    if backgroundMusic != nil {
        backgroundMusic.removeFromParent()
        if bgMusicAlarm != nil {
            bgMusicAlarm.removeFromParent()
        } else {
            bgMusicAlarm = SKAudioNode(fileNamed: "alarm.wav")
            bgMusicAlarm.autoplayLooped = true
            addChild(bgMusicAlarm)
        }
    }
    runAction(SKAction.playSoundFileNamed("SpaceGame.caf", waitForCompletion: false))
    self.backgroundMusic = SKAudioNode(fileNamed: name)
    self.backgroundMusic.autoplayLooped = true
    self.addChild(self.backgroundMusic)

}

This crashes for me as well, even with hula-semula’s latest suggestion.

I also tried to to clean the product, Xcode > Product > Clean, and also deleted all the Xcode derived data from ~/Library/Developer/Xcode/DerivedData

Looks like something else is going on with this, if multiple people are crashing the same way. Maybe an Xcode or SDK update issue??

All the same code works fine if I run with Xcode 7.2.1. You can download that version again from here:

actually We don’t need to add child or insert those two additional lines as I don’t have them in my code and it plays the background music perfect so maybe there is another issue at hand,if you like I will send you a complete copy of my game scene . swift file for you to compare with yours

Thanks for all comments fellas. Lets wait for a while and see if new updates will fix because it seems 7.3 update is the reason

DropCharge Background crash solution for xcode 7.3 is:

Firstly to add sounds file properly create a group named sounds and right click on it then click “Add files to” then select sound files .caf .wav etc. from resources. When directly dragging the resources to the environment it did not see the files as it shows the folder with not yellow but blue icon.

Secondly FOR CODİNG PART in the properties creat background music directly by typing

var backgroundMusic = SKAudioNode(fileNamed: “SpaceGame.caf”)

then change the function as

func playBackgroundMusic(){
self.backgroundMusic.autoplayLooped = true
self.addChild(backgroundMusic)
}

Call function in the didMoveToView

If you creat the backgroundMusic in the function instead of properties it crashes.

This temporarily gets me past the issue on page 444, but as you progress through it doesn’t fix the issue because you renamed the method so the other examples that send a file name parameter to that method needs to be addressed too.

Im Still Having this issue too but I fixed it by changing the method to this:

func playBackgroundMusic(name: String) {
if self.backgroundMusic != nil {
self.backgroundMusic.removeFromParent()
}
let backgroundMusic = SKAudioNode(fileNamed: name)
backgroundMusic.autoplayLooped = true
self.backgroundMusic = backgroundMusic
addChild(backgroundMusic)
}

This keeps the method working as before and the name is the same.

Yes this works either

Works for me as well. But has anyone gotten the alarm sound back in, as well?

This errors (nil found) for me if I uncomment the bgMusicAlarm code:

func playBackgroundMusic(name: String) {

    if self.backgroundMusic != nil {
        self.backgroundMusic.removeFromParent()
    
         /* if bgMusicAlarm != nil {
                bgMusicAlarm.removeFromParent()
            } else {
                bgMusicAlarm = SKAudioNode(fileNamed: "alarm.wav")
                bgMusicAlarm.autoplayLooped = true
                addChild(bgMusicAlarm)
            } */
    
    }
    
    let backgroundMusic = SKAudioNode(fileNamed: name)
    backgroundMusic.autoplayLooped = true
    self.backgroundMusic = backgroundMusic
    addChild(backgroundMusic)
}
func playBackgroundMusic(name:String){
    if backgroundMusic != nil {
        backgroundMusic.removeFromParent()
        if bgMusicAlarm != nil {
            bgMusicAlarm.removeFromParent()
        }
        else {
            let temp = SKAudioNode(fileNamed: "alarm.wav")
            temp.autoplayLooped = true
            bgMusicAlarm = temp
            addChild(bgMusicAlarm)
        }
    }
    let temp = SKAudioNode(fileNamed: name)
    temp.autoplayLooped = true
    backgroundMusic = temp
    addChild(backgroundMusic)
}
2 Likes

Excellent! That works. Thanks much

No problem bruv :grin:

Thanks for this “workaround”.