Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sound slider for cocos2d?

Any tutorials on how to make a slider for cocos2d that controls sounds that you guys recommend? Some of tutorials look a bit shady.

like image 673
Jhon Doe Avatar asked Nov 30 '25 04:11

Jhon Doe


1 Answers

You can use the slider provides by the CCControlExtension and use the callback method to change the volume of your sound as explained here.

Here a "pseudo" code to show you how to achieved this:

// Create your audio engine
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"music.mp3"];

// Create the slider
CCControlSlider *slider = [CCControlSlider sliderWithBackgroundFile:@"sliderTrack.png" progressFile:@"sliderProgress.png" thumbFile:@"sliderThumb.png"];
slider.minimumValue = 0.0f; // Sets the min value of range
slider.maximumValue = 1.0f; // Sets the max value of range

// When the value of the slider will change, the given selector will be call
[slider addTarget:self action:@selector(valueChanged:) forControlEvents:CCControlEventValueChanged];

[self addChild:slider]; 

//...

- (void)valueChanged:(CCControlSlider *)sender
{
   // Change volume of your sounds
   [[SimpleAudioEngine sharedEngine] setEffectsVolume:sender.value];
   [[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:sender.value];
}

I hope it'll help you.

like image 159
Yannick Loriot Avatar answered Dec 01 '25 22:12

Yannick Loriot