Search code examples
iphonexcode4cocos2d-iphone

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.


Solution

  • 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.