1

So basically, I play music on the first scene, and everything works fine. I transition to scene 2, and the music stops automatically, which is what I want. But then I transition back, and the music starts for half a second.. then just gets cut off for no reason. Just to make things more weird, it continues again perfectly only if I put my physical device on sleep mode, then unlock it.

I made an over-simplified code just to get straight to the problem I'm having in my real project. Here's my first scene, which simply plays music upon start up and transitions to scene 2 upon touching:

import SpriteKit

class GameScene: SKScene
{
    override func didMoveToView(view: SKView)
    {
        let backgroundMusic = SKAudioNode(fileNamed: "WhatIsLove.mp3")
        backgroundMusic.autoplayLooped = true
        backgroundMusic.positional = false
        addChild(backgroundMusic)
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let transition = SKTransition.revealWithDirection(.Down, duration: 1.0)
        let nextScene = SceneTwo(size: scene!.size)
        nextScene.scaleMode = .AspectFill
        nextScene.backgroundColor = UIColor.blueColor()
        scene?.view?.presentScene(nextScene, transition: transition)
    }
}

And here's scene 2, where the music automatically stops before transitioning in. Nothing here except transitioning back to scene 1 upon touching, where I want the music to start up again (but it stops playing after a half second):

import SpriteKit

class SceneTwo: SKScene
{
    override func didMoveToView(view: SKView)
    {
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let transition = SKTransition.revealWithDirection(.Down, duration: 1.0)
        let nextScene = GameScene(size: scene!.size)
        nextScene.scaleMode = .AspectFill
        nextScene.backgroundColor = UIColor.greenColor()
        scene?.view?.presentScene(nextScene, transition: transition)
    }
}

No idea why this is happening. I've tried several combinations with SKAudioNode and other ways to declare the variables and implement it. I've tried not using the autoplayLooped & positional options, and just about everything. I want the music to play again properly when I transition back to my first scene.

Hien
  • 101
  • 1
  • 1
  • 6

0 Answers0