Cocos2d-x instance: Set background music and sound effects-set scene implementation

Source: Internet
Author: User

Cocos2d-x instance: Set background music and sound effects-set scene implementation
The Setting. h file code is as follows:

#ifndef __Setting_SCENE_H__#define __Setting_SCENE_H__#include cocos2d.h#include SimpleAudioEngine.hclass Setting : public cocos2d::Layer{bool isEffect;①public:    static cocos2d::Scene* createScene();    virtual bool init();  virtual void onEnter();virtual void onEnterTransitionDidFinish();virtual void onExit();virtual void onExitTransitionDidStart();virtual void cleanup();        void menuSoundToggleCallback(cocos2d::Ref* pSender);void menuMusicToggleCallback(cocos2d::Ref* pSender);void menuOkCallback(cocos2d::Ref* pSender);        CREATE_FUNC(Setting);};#endif // __Setting_SCENE_H__#define __HELLOWORLD_SCENE_H__




The above code ① defines the Boolean member variable isEffect, which is used to save whether the current sound effect is allowed. In fact, whether the sound effect and background music playing status should be persisted in the file, since we have not introduced data persistence, we use isEffect variables to record the status. When we learn how to complete data persistence in the following sections, we will save their statuses. Other codes are very similar to those in the HelloWorld scenario. The Setting. cpp file code is as follows:
# Include SettingScene. hUSING_NS_CC; using namespace CocosDenshion ;...... Bool Setting: init () {if (! Layer: init () {return false;} log (Setting init); Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director :: getInstance ()-> getVisibleOrigin (); Sprite * bg = Sprite: create(setting-back.png); bg-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height/2); this-> addChild (bg); // sound auto soundOnMenuItem = MenuItemImage: create (on.png, on.png); auto soundOffMenuItem = MenuItemImage: create (off.png, off.png); auto scaling = MenuItemToggle: createWithCallback (CC_CALLBACK_1 (Setting: success, this), soundOffMenuItem, soundOnMenuItem, NULL); response-> setPosition (ctor: getInstance () -> convertToGL (Point (818,220); // background music auto musicOnMenuItem = MenuItemImage: create (on.png, on.png); auto musicOffMenuItem = MenuItemImage: create (off.png, off.png); auto increment = MenuItemToggle: createWithCallback (CC_CALLBACK_1 (Setting: success, this), musicOffMenuItem, musicOnMenuItem, NULL); increment-> setPosition (ctor: getInstance () -> convertToGL (Point (818,362); // OK button auto okMenuItem = MenuItemImage: create (ok-down.png, ok-up.png, CC_CALLBACK_1 (Setting: menuOkCallback, this )); okMenuItem-> setPosition (Director: getInstance ()-> convertToGL (Point (600,510); Menu * mn = Menu: create (soundToggleMenuItem, musicToggleMenuItem, okMenuItem, NULL ); mn-> setPosition (Point: ZERO); this-> addChild (mn); return true;} void Setting: menuOkCallback (Ref * pSender) {Director: getInstance () -> popScene (); if (isEffect) {SimpleAudioEngine: getInstance ()-> playEffect (sound/Blip.wav); ①} void Setting: menuSoundToggleCallback (Ref * pSender) ② {auto scaling = (MenuItemToggle *) pSender; log (soundToggleMenuItem % d, soundToggleMenuItem-> getSelectedIndex (); if (isEffect) {SimpleAudioEngine: getInstance () -> playEffect (sound/Blip.wav); ③} if (soundToggleMenuItem-> getSelectedIndex () = 1) {// select status Off-> On ④ isEffect = false ;} else {isEffect = true; SimpleAudioEngine: getInstance ()-> playEffect (sound/Blip.wav); ⑤ }} void Setting: menuMusicToggleCallback (Ref * pSender) ⑥ {auto scaling = (MenuItemToggle *) pSender; log (musicToggleMenuItem % d, response-> getSelectedIndex (); if (musicToggleMenuItem-> getSelectedIndex () = 1) {// selected status Off-> On 7 SimpleAudioEngine: getInstance ()-> stopBackgroundMusic (sound/synthaudio);} else {SimpleAudioEngine: getInstance () -> playBackgroundMusic (sound/synthmusic);} if (isEffect) {SimpleAudioEngine: getInstance ()-> playEffect (sound/Blip.wav); effect} void Setting: onEnter () {Layer: onEnter (); log (Setting onEnter);} void Setting: onEnterTransitionDidFinish () {Layer: onEnterTransitionDidFinish (); log (Setting onEnterTransitionDidFinish); isEffect = true; // play SimpleAudioEngine: getInstance ()-> playBackgroundMusic (sound/synthaudio, true); audio} void Setting: onExit () {Layer: onExit (); log (Setting onExit);} void Setting: onExitTransitionDidStart () {Layer: onExitTransitionDidStart (); log (Setting transaction);} void Setting: cleanup () {Layer :: cleanup (); log (Setting cleanup); // stop SimpleAudioEngine: getInstance ()-> stopBackgroundMusic (sound/synthmusic); done}


Line ①, ③ and ③ of the above Code are used to play the sound when the isEffect is determined to be true (the sound playback switch is enabled.
Line ② Of the code menuSoundToggleCallback is the callback function when the user clicks the sound development button. Line ④ of the code is used to determine whether the button status starts from Off-> On, if yes, set the switch variable isEffect to false; otherwise, set it to true, and play a sound effect once through line 5 code SimpleAudioEngine: getInstance ()-> playEffect (sound/Blip.wav.
Line 6 of the Code, menuMusicToggleCallback, is the callback function when the user clicks the background music button. The code line 7 is used to determine whether the status of the button is from Off-> On. If yes, the background music is stopped, otherwise, the playback starts.

The first line of code is to play background music in the onEnterTransitionDidFinish () function. The first line of code is to stop playing background music in the cleanup () function.

 

 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.