0

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")
                }
            }
        }  }

    }
Omer Harel
  • 283
  • 1
  • 3
  • 16
  • Be more specific... Are you saying that buttons work when you touch them but when you return to main screen (which is probably another scene) the wrong button texture is shown? – Whirlwind Apr 11 '16 at 12:40
  • Worng button texture. – Omer Harel Apr 11 '16 at 12:40
  • :) Okay, I figured that out already. I am asking you to be more specific about part of the code where you are having problems. So, does this code from touchesBegan works for you? If yes, what is the code which doesn't work. If no, well, I will look at it more closely... – Whirlwind Apr 11 '16 at 12:47
  • I will send link to video. What happend with my sound button. – Omer Harel Apr 11 '16 at 12:49
  • Alright... That would be helpful. – Whirlwind Apr 11 '16 at 12:50
  • https://youtu.be/bNXljUxexj4 – Omer Harel Apr 11 '16 at 12:58
  • Ah, so music on/off icon is not updated correctly. Well, that's why I asked about the code from the main scene. Basically, you have to check if music is off or on, and based on that to show appropriate texture. Right now, it seems that you are not making that check inside of a menu scene. – Whirlwind Apr 11 '16 at 13:04
  • You can write your answer (: – Omer Harel Apr 11 '16 at 13:16

1 Answers1

0

Basically, you have to check in your Menu scene if music is off or on, and based on that to show appropriate texture. Right now, it seems that you are not making that check inside of a menu scene.

HINT: Obviously, your Singleton class has been implemented as a singleton. And if everything is done correctly, it is instantiated once and alive through the whole app's life time. So it has an info about music on/off state. What I wanted to point is that, when the app is closed, this singleton gets deallocated. So before that happen, you might store the info about music on/off into persistent storage, like NSUserDefaults, so the next time when user open up the app, he has his settings saved. Just a though... This really depends on you and what you want to offer in your app.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • i want to save if is on or off and the next time when user open up the app, he has his settings saved. you can help me to write it – Omer Harel Apr 11 '16 at 13:59
  • @OmerHarel It is just simple reading / writing into user defaults. There are a lot of posts on SO about that... – Whirlwind Apr 11 '16 at 14:17
  • its all what i need to complete my game. – Omer Harel Apr 11 '16 at 14:26
  • @OmerHarel Read this answer. It is really simple. http://stackoverflow.com/a/31070325/3402095 Basically, you read from user defaults when app starts. Based on that info, you run the player using your `Singleton` class. After that, you use your app in a usual way (like you do now), means no more readings are needed from defaults. The next update of defaults is needed when the app is about to close. – Whirlwind Apr 11 '16 at 14:39
  • @Whirewind you sent me link and i edited my code here and its not working – Omer Harel Apr 11 '16 at 17:38
  • @OmerHarel If it is not working, then you have an error somewhere in your logic. That answer is correct. – Whirlwind Apr 11 '16 at 17:40
  • I'm using with texture in my button – Omer Harel Apr 11 '16 at 17:41