Book Image

Learning Cocos2d-JS Game Development

By : Emanuele Feronato
Book Image

Learning Cocos2d-JS Game Development

By: Emanuele Feronato

Overview of this book

Table of Contents (18 chapters)
Learning Cocos2d-JS Game Development
Credits
Foreword
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Creating Your Own Blockbuster Game – A Complete Match 3 Game
Index

Managing music and sound effects


It's time to create all the callback functions, so let's expand the content of the game class declaration:

var game = cc.Layer.extend({
  init:function () {
    // same as before
  },
  playSound:function(){
    this.audioEngine.playEffect("assets/bang.mp3");
  },
  playBGMusic:function(){
    if(!this.audioEngine.isMusicPlaying()){
      this.audioEngine.playMusic("assets/loop.mp3",true);
    }
  },
  stopBGMusic:function(){
    if(this.audioEngine.isMusicPlaying()){
      this.audioEngine.stopMusic();
    }
  },
  musicUp:function(){
    this.audioEngine.setMusicVolume(this.audioEngine.getMusicVolume()+0.1);
  },
  musicDown:function(){
    this.audioEngine.setMusicVolume(this.audioEngine.getMusicVolume()-0.1);
  },
  effectsUp:function(){
    this.audioEngine.setEffectsVolume(this.audioEngine.getEffectsVolume()+0.1);
  },
  effectsDown:function(){
    this.audioEngine.setEffectsVolume(this.audioEngine.getEffectsVolume()-0.1);
  }
});

Now, if you test the...