Shalin Shah
Shalin Shah

Reputation: 8183

Music toggle button objective-c

I made a toggle button in cocos2d located in the title screen of my game. I want this toggle to turn the music either on or off. This is what I have done so far:

I added the CCMenus and Sprites to the .h file:

 CCMenu *menu2;
 CCMenu *menutwo;
 CCMenuItemImage *sound;
 CCMenuItemSprite *soundOff;

Then I added the sound button and the NSUserDefaults in the init:

// Sound Button
sound = [CCMenuItemImage itemWithNormalImage:@"music.png" selectedImage:@"music.png" target:self selector:@selector(soundSettings)];
sound.scale = 1.1;
menu2 = [CCMenu menuWithItems:sound, nil];
menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
[self addChild:menu2];

if([[NSUserDefaults standardUserDefaults] objectForKey:@"musicon"] == nil) {
        [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"musicon"];
}

This is the soundSettings Method called above by the CCSprite:

- (void) soundSettings {
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"musicon"] == TRUE) {
        [self removeChild:soundOff cleanup:YES];
        [self removeChild:menutwo cleanup:YES];
        sound = [CCMenuItemImage itemWithNormalImage:@"music.png" selectedImage:@"music.png" target:self selector:@selector(soundIsOff)];
        sound.scale = 1.1;
        menu2 = [CCMenu menuWithItems:sound, nil];
        menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
        [self addChild:menu2];
    } else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"musicon"] == FALSE) {
        [self removeChild:sound cleanup:YES];
        [self removeChild:menu2 cleanup:YES];
        soundOff = [CCMenuItemImage itemWithNormalImage:@"music-not.png" selectedImage:@"music-not.png" target:self selector:@selector(soundIsOn)];
        soundOff.scale = 1.1;
        menutwo = [CCMenu menuWithItems:soundOff, nil];
        menutwo.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
        [self addChild:menutwo];
    }
}

These are the 2 methods being called by the toggle CCSprites:

-(void) soundIsOn {
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"musicon"];
}
-(void) soundIsOff {
    [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"musicon"];
}

Since this code is located in the Title Screen, this only stops the music for the title screen. As soon as I go to another screen, the music BOOL resets and becomes TRUE again. If anyone can help me out with this, I would really appreciate it. Thanks!

Note: This is how I will be checking to see if the music is on or off:

if([[NSUserDefaults standardUserDefaults]boolForKey:@"musicon"] == TRUE) {
   [[SimpleAudioEngine sharedEngine] playEffect:@"song.mp3"];
}

Upvotes: 2

Views: 315

Answers (1)

Janak Nirmal
Janak Nirmal

Reputation: 22726

Change your methods as follow, you need to synchronize after setting any values in NSUserDefaults to get again back saved value.

-(void) soundIsOn {
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"musicon"];
    [[NSUserDefaults standardUserDefaults] synchronize];// Add this 
}
-(void) soundIsOff {
    [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"musicon"];
    [[NSUserDefaults standardUserDefaults] synchronize];// Add this 
}

Upvotes: 3

Related Questions