Music and sound in the game are essential, good music can leave us a deep impression also determines the quality of the game. Today we'll learn how to use music and sound effects in cocos2d-x. The functions used are simpler and are now directly on the code.
Let's take a look at how to use the Sound engine bool Helloworld::init () {bool BRet = false; do {cc_break_if (!
Cclayer::init ());
Ccsize size = Ccdirector::shareddirector ()->getvisiblesize ();
Audiopretreat::sharedaudiopretreat ()->init ();
Here are some action//setting strings for background music, and transcoding std::string str1 = "Play background music";
std::string str2 = "Stop background music";
std::string STR3 = "Pause background music";
std::string STR4 = "Resume background music";
std::string STR5 = "Play back the background music";
std::string STR6 = "background music: Add";
std::string STR7 = "background music: minus";
GBKToUTF8 (str1, "GB2312", "UTF-8");
GBKToUTF8 (str2, "GB2312", "UTF-8");
GBKToUTF8 (STR3, "GB2312", "UTF-8");
GBKToUTF8 (STR4, "GB2312", "UTF-8");
GBKToUTF8 (STR5, "GB2312", "UTF-8");
GBKToUTF8 (STR6, "GB2312", "UTF-8");
GBKToUTF8 (STR7, "GB2312", "UTF-8");
Ccmenuitemfont::setfontsize (24);
Ccmenuitemfont::setfontname ("Arial");
Ccmenuitemfont * menu1 = Ccmenuitemfont::create (Str1.c_str (), This,menu_selector (Helloworld::menu));
Menu1->settag (1); Ccmenuitemfont * menu2 = ccmenuitemfont::create (str2.c_str(), This,menu_selector (Helloworld::menu));
Menu2->settag (2);
Ccmenuitemfont * menu3 = Ccmenuitemfont::create (Str3.c_str (), This,menu_selector (Helloworld::menu));
Menu3->settag (3);
Ccmenuitemfont * Menu4 = Ccmenuitemfont::create (Str4.c_str (), This,menu_selector (Helloworld::menu));
Menu4->settag (4);
Ccmenuitemfont * Menu5 = Ccmenuitemfont::create (Str5.c_str (), This,menu_selector (Helloworld::menu));
Menu5->settag (5);
Ccmenuitemfont * Menu6 = Ccmenuitemfont::create (Str6.c_str (), This,menu_selector (Helloworld::menu));
Menu6->settag (6);
Ccmenuitemfont * Menu7 = Ccmenuitemfont::create (Str7.c_str (), This,menu_selector (Helloworld::menu));
Menu7->settag (7);
Ccmenu * Leftmenu = ccmenu::create (menu1,menu2,menu3,menu4,menu5,menu6,menu7,null);
Leftmenu->alignitemsverticallywithpadding (5);
Leftmenu->setpositionx (SIZE.WIDTH/4);
This->addchild (Leftmenu);
The following is the operation of the sound effects std::string str11 = "Play sound effects";
std::string str12 = "Stop sound effect"; std::string Str13 = "Pause sound Effects ";
std::string str14 = "restore sound effect";
std::string str15 = "Remove sound from cache";
std::string str16 = "Sound effects: Add";
std::string str17 = "sound effect: minus";
GBKToUTF8 (Str11, "GB2312", "UTF-8");
GBKToUTF8 (Str12, "GB2312", "UTF-8");
GBKToUTF8 (Str13, "GB2312", "UTF-8");
GBKToUTF8 (Str14, "GB2312", "UTF-8");
GBKToUTF8 (Str15, "GB2312", "UTF-8");
GBKToUTF8 (str16, "GB2312", "UTF-8");
GBKToUTF8 (Str17, "GB2312", "UTF-8");
Ccmenuitemfont * Menu11 = Ccmenuitemfont::create (Str11.c_str (), This,menu_selector (Helloworld::menu));
Menu11->settag (11);
Ccmenuitemfont * Menu12 = Ccmenuitemfont::create (Str12.c_str (), This,menu_selector (Helloworld::menu));
Menu12->settag (12);
Ccmenuitemfont * menu13 = Ccmenuitemfont::create (Str13.c_str (), This,menu_selector (Helloworld::menu));
Menu13->settag (13);
Ccmenuitemfont * menu14 = Ccmenuitemfont::create (Str14.c_str (), This,menu_selector (Helloworld::menu));
Menu14->settag (14); Ccmenuitemfont * menu15 = Ccmenuitemfont::create (Str15.c_str (), This,menu_selector (helloworlD::menu));
Menu15->settag (15);
Ccmenuitemfont * menu16 = Ccmenuitemfont::create (Str16.c_str (), This,menu_selector (Helloworld::menu));
Menu16->settag (16);
Ccmenuitemfont * Menu17 = Ccmenuitemfont::create (Str17.c_str (), This,menu_selector (Helloworld::menu));
Menu17->settag (17);
Ccmenu * Rightmenu = ccmenu::create (menu11,menu12,menu13,menu14,menu15,menu16,menu17,null);
Rightmenu->alignitemsverticallywithpadding (5);
Rightmenu->setpositionx (SIZE.WIDTH*3/4);
This->addchild (Rightmenu);
BRet = true;
while (0);
return bRet;
} void Helloworld::menu (Ccobject * sender) {Ccnode * node = (Ccnode *) sender;
int tag = Node->gettag (); Switch (TAG) {Case 1://play sound, and the second parameter indicates whether to loop Simpleaudioengine::sharedengine ()->playbackgroundmusic (audiopretre
At::sharedaudiopretreat ()->getmusic (Music_type_background), true);
Break
Case 2:simpleaudioengine::sharedengine ()->stopbackgroundmusic ();
Break Case 3://pause and resume used in pairs SimpleaUdioengine::sharedengine ()->pausebackgroundmusic ();
Break
Case 4:simpleaudioengine::sharedengine ()->resumebackgroundmusic ();
Break
Case 5:simpleaudioengine::sharedengine ()->rewindbackgroundmusic ();
Break Case 6://This function can not change the size of the sound on the Win32, we use the F12 follow-up function when the inside is empty, but on the Android platform has the implementation of Simpleaudioengine::sharedengine ()->
Setbackgroundmusicvolume (Simpleaudioengine::sharedengine ()->getbackgroundmusicvolume () +0.1f);
Break Case 7://This function can not change the size of the sound on the Win32, we use the F12 follow-up function when the inside is empty, but on the Android platform has the implementation of Simpleaudioengine::sharedengine ()->
Setbackgroundmusicvolume (Simpleaudioengine::sharedengine ()->getbackgroundmusicvolume () -0.1f);
Break Case 11://The second parameter is set to True then loop, Soundid is defined in HelloWorld header file//The sound function differs from the background music by returning an id Soundid = simpleaudioengine::shared
Engine ()->playeffect (Audiopretreat::sharedaudiopretreat ()->geteffect (Effect_type_effect1), true);
Break Case 12://The sound function will mostly pass in an ID, because the sound effects in the game are more than one simpleaudioengine::sharedengine ()->stopeffect (sOundid);
Break
Case 13:simpleaudioengine::sharedengine ()->pauseeffect (Soundid);
Break
Case 14:simpleaudioengine::sharedengine ()->resumeeffect (Soundid); Case 15://removing sound effects from the cache, the name of the Simpleaudioengine::sharedengine ()->unloadeffect () is required to remove the sound effect (audiopretreat::sharedaudiopre
Treat ()->geteffect (Effect_type_effect1)); Case 16:simpleaudioengine::sharedengine ()->seteffectsvolume (Simpleaudioengine::sharedengine ()->
Geteffectsvolume () +0.1);
Break Case 17:simpleaudioengine::sharedengine ()->seteffectsvolume (Simpleaudioengine::sharedengine ()->
Geteffectsvolume ()-0.1);
Break
}//encoding format conversion function int GBKToUTF8 (std::string & gbkstr, const char* tocode, const char* fromcode) {iconv_t ICONVH;
ICONVH = Iconv_open (Fromcode, Tocode);
if (ICONVH = = 0) {return-1;
} const char* Strchar = Gbkstr.c_str ();
Const char** PIN = &strChar;
size_t strlength = Gbkstr.length ();
char* outbuf = (char*) malloc (strlength*4);
char* pbuff = outbuf;
memset (outbuf, 0, strlength*4);
size_t outlength = strlength*4;
if ( -1 = = Iconv (ICONVH, PIN, &strlength, &OUTBUF, &outlength)) {iconv_close (ICONVH);
return-1;
} gbkstr = Pbuff;
Iconv_close (ICONVH);
return 0; }