I have Sound button (On/Off) in main scene and its works perfectly but I have problem , off the background music playing when I was disqualified I return to the main screen, but the image of the button changes the music is turned on and stays on the image of the music is off.
Main Scene :
var SoundOnOff = SKSpriteNode()
override func didMoveToView(view: SKView) {
backgroundColor = UIColor(red:0.09, green:0.63, blue:0.52, alpha:1.0)
//Main Scene:
SoundOnOff.texture = SKTexture(imageNamed:"Sound-on.png")
SoundOnOff.position = CGPoint(x: self.size.width/2 - 40 , y: self.size.height/2 - 500)
SoundOnOff.size = CGSizeMake(60 , 60)
SoundOnOff.runAction(SKAction.moveToY(140, duration: 0.5))
SoundOnOff.removeFromParent()
addChild(SoundOnOff)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let location = touch!.locationInNode(self)
if(SoundOnOff.containsPoint(location)) {
// ---------------------------------------------
// Play Background Music
// ---------------------------------------------
if ((NSUserDefaults.standardUserDefaults().objectForKey("onoroff")) !== true)
{
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
Singleton.sharedInstance().pauseBackgroundMusic()
SoundOnOff.texture = SKTexture(imageNamed:"Sound-off.png")
}else {
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
Singleton.sharedInstance().resumeBackgroundMusic()
SoundOnOff.texture = SKTexture(imageNamed:"Sound-on.png")
}
}
} }
}