cocos2d-x学习笔记(5)-- CCScene场景的切换
[cpp]
cocos2d-x学习笔记(5)-- CCScene场景的切换
step1:创建一个cocos2d-win32 application,并命名为易做图Game;
step:为了代码的整洁,我把上一节中的山和太阳这两个背景合为一个背景---LayerDay(白天),同时将山和月亮合并为NightLayer夜景(晚上)
效果如下两张图
白天:
晚上:
在HelloWorldScene.h中添加LayerDay 、LayerNight和MyScene三个类:
[cpp]
/************************************************************************/
/* 白天的背景 */
/************************************************************************/
class LayerDay:public CCLayerColor
{
public:
LayerDay();
~LayerDay();
public:
virtual void onEnter();
};
/************************************************************************/
/* 晚上的背景 */
/************************************************************************/
class LayerNight:public CCLayerColor
{
public:
LayerNight();
~LayerNight();
public:
virtual void onEnter();
};
/************************************************************************/
/* 自定义场景 */
/************************************************************************/
class MyScene:public CCScene
{
public:
MyScene();
public:
virtual void onEnter();
virtual void runThisTest();
void daySceneCallback(CCObject* pSender);
void nightSceneCallback(CCObject* pSender);
};
在HelloWorldScene.cpp中修改HelloWorld::scene()如下:
[cpp]
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = new MyScene();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
//HelloWorld *layer = HelloWorld::node();
LayerDay* pLayer = new LayerDay();
// add layer as a child to scene
//addChild中的第二个参数为背景的次序,LayerHill在LayerCloud的上面,即表面。
scene->addChild(pLayer,0);
} while (0);
// return the scene
return scene;
}
然后添加三个类的成员函数:
[cpp]
/************************************************************************/
/* class LayerNight */
/************************************************************************/
LayerNight::LayerNight()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
this->initWithColor(ccc4(0,0,0,255));
CCSprite* pSpriteNight = CCSprite::spriteWithFile("night.png");
CCSprite* pSpriteHill = CCSprite::spriteWithFile("hill.png");
pSpriteNight->setPosition(ccp(size.width/2,size.height/2));
pSpriteHill->setPosition(ccp(size.width/2,size.height/2));
addChild(pSpriteNight);
addChild(pSpriteHill);
}
LayerNight::~LayerNight()
{
}
void LayerNight::onEnter()
{
CCLayer::onEnter();
}
/************************************************************************/
/* class LayerDay */
/************************************************************************/
LayerDay::LayerDay ()
{
this->initWithColor(ccc4(255,255,255,255));
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* pSpriteHill = CCSprite::spriteWithFile("hill.png");
CCSprite* pSpriteCloud = CCSprite::spriteWithFile("cloud.png");
pSpriteCloud->setPosition(ccp(size.width/2,size.height/2));
pSpriteHill->setPosition(ccp(size.width/2,size.height/2));
addChild(pSpriteCloud);
addChild(pSpriteHill);
}
LayerDay ::~LayerDay ()
{
}
补充:移动开发 , 其他 ,