I have two scenes and I want that the background music play in both. The problem is that the music plays only the first time the app opens. If I press the button to go to the second scene the music plays while the scenes transition, but stop when the transition end. No music for the second scene and if I go back to the first scene there is no music there too. The music always play during the transition between scenes (this is why I'm using a long transition time, just to confirm (I don't know why).
class GameScene: SKScene {
var backgroundMusic: SKAudioNode!
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.redColor()
global.centerPoint = CGPoint(x:(CGRectGetMidX(self.frame)), y:(CGRectGetMidY(self.frame)))
let myLabel = SKLabelNode(fontNamed:"Helvetica Bold")
myLabel.text = "GameScene"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + 100)
self.addChild(myLabel)
createBtn()
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// MUSICA
if let musicURL = NSBundle.mainBundle().URLForResource("music", withExtension: "wav") {
backgroundMusic = SKAudioNode(URL: musicURL)
addChild(backgroundMusic)
print("backgroundMusic OK")
} else {
print("backgroundMusic file not found!")
}
} // END override func didMoveToView
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch = touches //as! Set<UITouch>
let location = touch.first!.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "btnOne") {
let reveal = SKTransition.pushWithDirection(.Left, duration: 2.5)
let scene = SceneTwo(size: size)
self.view?.presentScene(scene, transition:reveal)
}
} // END override func touchesBegan
For the second file/scene I'm using the same code:
class SceneTwo: SKScene {
var backgroundMusicTwo: SKAudioNode!
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.blueColor()
global.centerPoint = CGPoint(x:(CGRectGetMidX(self.frame)), y:(CGRectGetMidY(self.frame)))
let myLabel = SKLabelNode(fontNamed:"Helvetica Bold")
myLabel.text = "SceneTwo"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + 100)
self.addChild(myLabel)
createBtn()
// # # # # # # # # # # # # # # # # # # # # # # # # # # #
// MUSICA
if let musicURL = NSBundle.mainBundle().URLForResource("music", withExtension: "wav") {
backgroundMusicTwo = SKAudioNode(URL: musicURL)
addChild(backgroundMusicTwo)
print("backgroundMusicTwo OK")
} else {
print("backgroundMusicTwo file not found!")
}
} // END override func didMoveToView