"COCOS2DX" Resource folder, play background music, import external library

Source: Internet
Author: User
Tags addchild

Playing background music in COCOS2DX is a very easy thing to do, just one line of code, but first import the COCOS2DX audio engine cocosdenshion. Cocosdenshion provides a very convenient voice material for the COCOS2D project to call and manage. Also put the music you want to play to the project's resource folder to play it correctly

and "COCOS2DX" Windows platform under the COCOS2DX 2.x download, installation, configuration, create their own HelloWorld "(Click to open the link) also through the following Python command, create a BGM project:

Python create_project.py-project bgm-package test.bgm-language cpp

You can find the resources folder in this project folder, and you can find that the content is the Helloworld.png and close button that we have already played.

COCOS2DX all pictures, music resources must be in the folder to be able to be called.


We copy one instance of Windows to bring music Kalimba.mp3 in, let our COCOS2DX program at startup, automatically loop this Kalimba.mp3 music, that is, the game's BGM.

Open the HelloCpp.sln in Proj.win32 directly.

For Hellocpp or only in HelloWorld this scene play BGM, then click on its properties, such as adding COCOS2DX audio engine cocosdenshion the required package. is to put. (COCOS2DX) \cososdenshion\include This folder full introduction to this project or this CPP.


After clicking OK, we only need to introduce the header file # include "SimpleAudioEngine.h" in the HelloWorldScene.cpp at the same time in bool Helloworld::init () {} Add a line to play the background music code on it. The final HelloWorldScene.cpp code changes as follows, actually did not change anything.

#include "HelloWorldScene.h" #include "SimpleAudioEngine.h"//Introduction header file using_ns_cc;        ccscene* Helloworld::scene () {//' scene ' is an Autorelease object ccscene *scene = Ccscene::create ();    ' Layer ' is an Autorelease object HelloWorld *layer = Helloworld::create ();    Add layer as a child to scene scene->addchild (layer); Return the scene return scene;} On "Init" need to initialize your Instancebool helloworld::init () {Cocosdenshion::simpleaudioengine::sharedengine ()->playbackgroundmusic ("Kalimba.mp3", true);//Play Music//differ from cocosdenshion::simpleaudioengine::sharedengine ()- >playeffect ("Xx.wav"), designed to play short sound effects////////////////////////////////1. Super init first if (!    Cclayer::init ()) {return false;    } ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize ();    Ccpoint origin = Ccdirector::shareddirector ()->getvisibleorigin (); 2. Add a menu item with "X" image, which is clickEd to quit the program//may modify it. Add a "Close" icon to exit the progress.                                        It ' s an Autorelease object ccmenuitemimage *pcloseitem = Ccmenuitemimage::create ( "Closenormal.png", "closeselected.png", th    IS, Menu_selector (Helloworld::menuclosecallback));                                Pcloseitem->setposition (CCP (origin.x + visiblesize.width-pcloseitem->getcontentsize (). WIDTH/2,    ORIGIN.Y + pcloseitem->getcontentsize (). HEIGHT/2));    Create menu, it's an Autorelease object ccmenu* pmenu = Ccmenu::create (Pcloseitem, NULL);    Pmenu->setposition (Ccpointzero);    This->addchild (Pmenu, 1); 3. Add your codes below ...//Add a label shows "Hello World"//Create and initialize a label cclabelttf* Plabe L = cclabelttf::create ("Hello World", "ArIal ", 24);                            Position the label on the center of the Plabel->setposition (CCP (origin.x + VISIBLESIZE.WIDTH/2,    ORIGIN.Y + visiblesize.height-plabel->getcontentsize (). height));    Add the label as a child to this layer This->addchild (Plabel, 1);    Add "HelloWorld" splash screen "ccsprite* psprite = ccsprite::create (" Helloworld.png "); Position the sprite on the center of the Psprite->setposition (CCP (VISIBLESIZE.WIDTH/2 + origin.x, visibles    IZE.HEIGHT/2 + origin.y));        Add the sprite as a child to this layer This->addchild (psprite, 0); return true;} void Helloworld::menuclosecallback (ccobject* psender) {#if (Cc_target_platform = = CC_PLATFORM_WINRT) | | (Cc_target_platform = = CC_PLATFORM_WP8) Ccmessagebox ("You pressed the Close button. Windows Store Apps do not implement a close button. "," "Alert"); #else ccdirector::shareddirector ()->end (); #if (Cc_targ Et_platform = = Cc_platform_ios) exiT (0); #endif #endif} 

Running this program, you can hear Kalimba.mp3, the nasty Windows7 example music. where Cocosdenshion::simpleaudioengine::sharedengine ()->playbackgroundmusic ("Kalimba.mp3", true); The first parameter in this music is the path to the resource folder, and True represents the loop playback.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"COCOS2DX" Resource folder, play background music, import external library

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.