播放背景音乐,实时音乐
- #include “SimpleAudioEngine.h”
- CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(“pew-pew-lei.wav”); // 实时音乐
- CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(“background-music-aac.wav”, true);// 循环播放背景音
创建一个自己的场景
可以实现场景之间的切换,可以实现延时动作。
// GameOverLayer.h
include “cocos2d.h”
class GameOverLayer :public cocos2d::CCLayerColor
{
public:
cocos2d::CCLabelTTF * _label;
bool init ();
~ GameOverLayer();
static cocos2d ::CCScene* scene();
CREATE_FUNC(GameOverLayer );
void returnToGame (cocos2d:: CCNode*);
// CCLabelTTF::create() HelloWorld::create()
};
// GameOverLayer.cpp
include “GameOverLayer.h” ;
include “HelloWorldScene.h” ;
USING_NS_CC;
bool GameOverLayer ::init()
{
if (CCLayerColor ::initWithColor( ccc4(0, 255, 0, 255)) != NULL )
{
_label = CCLabelTTF ::create( “word”, “Artial” , 40);
CCSize size = CCDirector:: sharedDirector()->getVisibleSize ();
_label->setPosition (ccp(size.width / 2, size.height / 2));
this->addChild (_label);;
_label->retain (); //保证_label 跟类对象有同样的生命周期
CCDelayTime* delay = CCDelayTime:: create(5);//延迟5秒 // 执行一个延时动作
CCCallFuncN* returnToGame = CCCallFuncN:: create(this , callfuncN_selector(GameOverLayer ::returnToGame));
this->runAction (CCSequence:: create(delay , returnToGame, NULL));
return true ;
}
return false ;
}
void GameOverLayer ::returnToGame( CCNode* )
{
CCDirector::sharedDirector ()->replaceScene( HelloWorld::scene ()); // 场景之间切换
}
GameOverLayer::~GameOverLayer ()
{
if (_label )
{
_label->release ();
}
}
cocos2d::CCScene * GameOverLayer:: scene()
{
CCScene* scene = CCScene:: create();
GameOverLayer* layer = GameOverLayer:: create();
layer->setTag (100);
scene->addChild (layer);
return scene ;
}