Note that there are some explanatory texts on larger screens.

plurals
  1. POMusic toggle button objective-c
    text
    copied!<p>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:</p> <p>I added the CCMenus and Sprites to the .h file:</p> <pre><code> CCMenu *menu2; CCMenu *menutwo; CCMenuItemImage *sound; CCMenuItemSprite *soundOff; </code></pre> <p>Then I added the sound button and the NSUserDefaults in the init:</p> <pre><code>// 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"]; } </code></pre> <p>This is the soundSettings Method called above by the CCSprite:</p> <pre><code>- (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]; } } </code></pre> <p>These are the 2 methods being called by the toggle CCSprites:</p> <pre><code>-(void) soundIsOn { [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"musicon"]; } -(void) soundIsOff { [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"musicon"]; } </code></pre> <p>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!</p> <p><strong>Note:</strong> This is how I will be checking to see if the music is on or off:</p> <pre><code>if([[NSUserDefaults standardUserDefaults]boolForKey:@"musicon"] == TRUE) { [[SimpleAudioEngine sharedEngine] playEffect:@"song.mp3"]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload