cocos2d-x学习笔记(5)-- CCScene场景的切换

来源:未知 责任编辑:智问网络 发表时间:2013-10-07 00:58 点击:

[cpp] 
  

cocos2d-x学习笔记(5)-- CCScene场景的切换
step1:创建一个cocos2d-win32 application,并命名为simpleGame;
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 () 

 

 
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);
  //......

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

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • cocos2d-x学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1