user618175
user618175

Reputation: 33

Muting sound problem cocos2d

Im having a slight problem with a settings scene in cocos2d, ive set it up so i can pause and resume some sound by touching a button.

-(id)init{
if((self=[super init])){

    //menu background
    CCSprite *bg = [CCSprite spriteWithFile:@"settingsbackground.jpg"];
    bg.anchorPoint = ccp(0, 0);
    bg.position = ccp(0, 0);
    [self addChild:bg z:0];

    CCMenuItem *Back = [CCMenuItemImage itemFromNormalImage:@"backbutton.gif" selectedImage: @"backbutton.gif" target:self selector:@selector(backToMainMenu:)];
    CCMenuItem *On = [CCMenuItemImage itemFromNormalImage:@"soundon1.gif" selectedImage: @"soundon1.gif" target:self selector:@selector(musicOn:)];
    CCMenuItem *Off = [CCMenuItemImage itemFromNormalImage:@"soundoff1.png" selectedImage: @"soundoff1.png" target:self selector:@selector(musicOff:)];

    CCMenu *BackButton = [CCMenu menuWithItems: Back, nil];
    BackButton.position = ccp(25, 295);

    CCMenu *ToggleButtonOn = [CCMenu menuWithItems: On, nil];
    ToggleButtonOn.position = ccp(25, 195);

    CCMenu *ToggleButtonOff = [CCMenu menuWithItems: Off, nil];
    ToggleButtonOff.position = ccp(25, 95);

    [self addChild:BackButton z:1000];
    [self addChild:ToggleButtonOn];
    [self addChild:ToggleButtonOff];


}
return self;

}

}

The problem is when i go back onto the menu the sound starts again even though its muted on the settings scene.

Whats the best way to get around this?

Cheers

Upvotes: 2

Views: 886

Answers (1)

Amaldeep Prakash
Amaldeep Prakash

Reputation: 35

Create an appdelegate variable which stores the current volume status.And dont forget to include setBackgroundMusicVolume code in init of each scene.

something like :

[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:appDelegate.volume ];

where appdelegate.volume is my appdelegate volume variable.When you are muting set the variable to 0,when unmuting to 1.

It works

Upvotes: 1

Related Questions