2

任何关于如何为 cocos2d 制作一个控制声音的滑块的教程,你们推荐吗?有些教程看起来有点阴暗。

4

1 回答 1

10

您可以使用CCControlExtension提供的滑块并使用回调方法来更改声音的音量,如此所述。

这里有一个“伪”代码向您展示如何实现这一点:

// 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];
}

我希望它会帮助你。

于 2012-06-29T13:18:08.827 回答