Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controlling volume, pitch, and balance


You can control the volume, pitch, and balance for sound effects. The right blend of these three factors makes your game sound more fun.

How to do it...

Let's try to immediately play a sound effect by controlling its volume, pitch, and balance. The following is the code snippet to do so:

auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); 
// set volume 
audio->setEffectsVolume(0.5); 

// set pitch, pan, gain with playing a sound effect.
float pitch = 1.0f;
float pan = 1.0f;
float gain = 1.0f;
audio->playEffect(EFFECT_FILE, true, pitch, pan, gain);

How it works...

You can control the volume for sound effects by using the setEffectsVolume method. The maximum value for the volume is 1.0, and the minimum value is 0.0. If you set the volume to 0.0, the sound effect is muted. The default value of the volume is 1.0.

You can play multiple sound effects at the same time, but you cannot set the volume for these effects individually. To change the master...