step1:创建一个cocos2d-win32 application,并命名为simpleGame;

step:为了代码的整洁,我把上一节中的山和太阳这两个背景合为一个背景---LayerDay(白天),同时将山和月亮合并为NightLayer夜景(晚上)

效果如下两张图

白天:

晚上:

在HelloWorldScene.h中添加LayerDay 、LayerNight和MyScene三个类:

/************************************************************************/
/* 白天的背景                                                                     */
/************************************************************************/
 
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()如下:

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;
}

然后添加三个类的成员函数:

/************************************************************************/
/* 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 ()
{
 
}
 
void LayerDay ::onEnter()
{
  CCLayer::onEnter();
}
 
 
/************************************************************************/
/* 自定义场景                                                               */
/************************************************************************/
MyScene::MyScene()
{
  CCScene::init();
  CCLabelTTF* labelDay = CCLabelTTF::labelWithString("Day", "Arial",20);
  CCLabelTTF* labelNight = CCLabelTTF::labelWithString("Night", "Arial",20);
 
  CCMenuItemLabel* pMenuDayItem = CCMenuItemLabel::itemWithLabel(
    labelDay, this,menu_selector(MyScene::daySceneCallback));
  CCMenuItemLabel* pMenuNightItem = CCMenuItemLabel::itemWithLabel(
    labelNight, this,menu_selector(MyScene::nightSceneCallback));
  CCMenu* pMenuDay = CCMenu::menuWithItems(pMenuDayItem, NULL);
  CCMenu* pMenuNight = CCMenu::menuWithItems(pMenuNightItem, NULL);
 
  CCSize size = CCDirector::sharedDirector()->getWinSize();
  pMenuDayItem->setPosition(ccp(size.width/2 - 40, 30));
  pMenuNightItem->setPosition(ccp(size.width/2 + 40, 30));
  pMenuDay->setPosition(CCPointZero);
  pMenuNight->setPosition(CCPointZero);
 
  addChild(pMenuDay,2);
  addChild(pMenuNight,2);
}
 
void MyScene::daySceneCallback(CCObject *pSender)
{
  CCScene* scene =new MyScene();
  CCLayer* pLayer = new LayerDay();
  scene->addChild(pLayer, 0);
  CCDirector::sharedDirector()->pushScene(scene);
  scene->release();
  pLayer->release();
}
 
void MyScene::nightSceneCallback(CCObject* pSender)
{
  CCScene* scene =new MyScene();
  CCLayer* pLayer = new LayerNight();
  scene->addChild(pLayer, 0);
  CCDirector::sharedDirector()->pushScene(scene);
  scene->release();
  pLayer->release();
}
 
void MyScene::onEnter()
{
  CCScene::onEnter();
}
 
void MyScene::runThisTest()
{
  CCLayer* pLayer = new LayerNight();
 
  addChild(pLayer);
  pLayer->release();
 
  CCDirector::sharedDirector()->replaceScene(this);
}

step3:编译运行程序,就会出现如下画面,通过点击画面上的 "Day"和"Night"两个字样,就可以对场景进行切换。

但是,场景在切换时给人感觉很生硬,要想让场景间过度得更更自然、更绚丽,我们在HelloWorldScene.cpp中对代码做一些修改:

首先添加:

CCTransitionScene* createTransition(cocos2d::ccTime t, CCScene* s)
{
 return CCTransitionFadeUp::transitionWithDuration(t, s);//此处是场景切换时的效果

    //return CCTransitionFadeDown::transitionWithDuration(t, s);
    //return CCTransitionTurnOffTiles::transitionWithDuration(t, s);
    //return CCTransitionSplitRows::transitionWithDuration(t, s);
    //return CCTransitionSplitCols::transitionWithDuration(t, s);

  //......


}

再次运行程序,这次效果是不是好多了呢!

 

源代码下载地址:http://download.csdn.net/download/wen294299195/4525796

打赏

发表评论

您的电子邮箱地址不会被公开。