Book Image

Building Android Games with Cocos2d-x

By : Raydelto Hernandez
Book Image

Building Android Games with Cocos2d-x

By: Raydelto Hernandez

Overview of this book

Table of Contents (16 chapters)
Building Android Games with Cocos2d-x
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a mute button to our game


Before ending this chapter, we are going to add a mute button to our game so that we can simply set our sound effects and our background music volume to zero in a single touch.

In order to achieve this, we are going to add two methods to the HelloWorld class; one for initializing the buttons and the other for actually muting all the sounds.

In order to achieve this, we are going to add the following lines to our HelloWorldScene.h header file under the private section:

int _musicId;
cocos2d::MenuItemImage* _muteItem;
cocos2d::MenuItemImage* _unmuteItem;
void initMuteButton();
void muteCallback(cocos2d::Ref* pSender);

Now, we are going to add the initMuteButton implementation code to the HelloWorldScene.cpp file as follows:

void HelloWorld::initMuteButton()
{
   _muteItem = MenuItemImage::create("mute.png", "mute.png", CC_CALLBACK_1(HelloWorld::muteCallback, this));    
   
   _muteItem->setPosition(Vec2(_visibleSize.width - _muteItem-  >getContentSize().width...