Then describe the basic knowledge of Cocos2D-x.
This section describes the Cocos2D-x music play function.
It is also slightly transformed in HelloWorld, and there are a lot of code:
Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
//////////////////////////////////////// //////////////////////////////////
// Super init first
//////////////////////////////////////// //////////////////////////////////
CC_BREAK_IF (! CCLayer: init ());
//////////////////////////////////////// //////////////////////////////////
// Add your codes below...
//////////////////////////////////////// //////////////////////////////////
CCLabelTTF * pLabel = CCLabelTTF: create ("Play \ BlackMusic", "Arial", 25 );
CCLabelTTF * pLabel1 = CCLabelTTF: create ("stop \ BlackMusic", "Arial", 25 );
CCMenuItemLabel * label = CCMenuItemLabel: create (pLabel, this, menu_selector (HelloWorld: play ));
CCMenuItemLabel * label1 = CCMenuItemLabel: create (pLabel1, this, menu_selector (HelloWorld: stop ));
Label-> setPosition (CCDirector: shareddire()-> getWinSize (). width/2, CCDirector: shareddire()-> getWinSize (). height/2-10 );
Label1-> setPosition (CCDirector: shareddire()-> getWinSize (). width/2, CCDirector: shareddire()-> getWinSize (). height/2-30 );
CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("CloseNormal.png", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback ));
CC_BREAK_IF (! PCloseItem );
PCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20, 20 ));
CCMenu * pMenu = CCMenu: create (pCloseItem, label, label1, NULL );
PMenu-> alignItemsVertically ();
PMenu-> setPosition (CCDirector: shareddire()-> getWinSize (). width/2, CCDirector: shareddire()-> getWinSize (). height/2 );
CC_BREAK_IF (! PMenu );
This-> addChild (pMenu, 1 );
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("Karunesh-Follow Your heart#", true );
BRet = true;
} While (0 );
Return bRet;
}
Void HelloWorld: menuCloseCallback (CCObject * pSender)
{
// "Close" menu item clicked
CCDirector: sharedDirector ()-> end ();
}
Void HelloWorld: play (CCObject * pSender)
{
// "Close" menu item clicked
If (CocosDenshion: SimpleAudioEngine: sharedEngine ()-> isBackgroundMusicPlaying ())
{
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("beatitmusic", true );
}
}
Void HelloWorld: stop (CCObject * pSender)
{
If (CocosDenshion: SimpleAudioEngine: sharedEngine ()-> isBackgroundMusicPlaying ())
{
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();
}
}
This instance has the following simple results:
Source code: http://download.csdn.net/detail/vanquishedzxl/7047355