[Cocos2d-x For WP8]Scene场景

     场景(CCScene) 在游戏里,场景就是关卡。 CCScene是app工作流程上独立块,一个app可能拥有多个scene,但是在任何时候只能有一个是激活状态的。一个CCScene对象由一个或多个CCLayer组成,layer之间是前后相连的。Layer提供了scene的外观和行为。通常的用法是直接用一个或多个layer实例化一个scene。CCScene的子类CCTrasitionScene实现移动transitions,提供两个scene之间的转移效果,由于scene是CCNode的子类,所以scene可以手工或者用aciton来转变坐标。所有层和精灵都是他的(子节点)children。

CCScene的4个基本方法:

runWithScene(运行画面),

replaceScene(替换场景 例如从主菜单进入游戏界面),

pushScene(暂停界面),

popScene(从暂停的界面还原到游戏界面)

示例:

class SceneTestLayer1 : public cocos2d::CCLayer
{
public:
    SceneTestLayer1();
    ~SceneTestLayer1();

    virtual void onEnter();
    virtual void onEnterTransitionDidFinish();
    void onPushScene(cocos2d::CCObject* pSender);
    void onPushSceneTran(cocos2d::CCObject* pSender);
} ;

class SceneTestLayer2 : public cocos2d::CCLayer
{
public:
    SceneTestLayer2();
    void onGoBack(cocos2d::CCObject* pSender);
} ;

SceneTestLayer1::SceneTestLayer1()
{
    CCMenuItemFont* item1 = CCMenuItemFont::create( "Test pushScene", this, menu_selector(SceneTestLayer1::onPushScene) );
    CCMenuItemFont* item2 = CCMenuItemFont::create( "Test pushScene w/transition", this, menu_selector(SceneTestLayer1::onPushSceneTran) );
    CCMenu* menu = CCMenu::create( item1, item2, NULL );
    menu->alignItemsVertically();    
    addChild( menu );
}

void SceneTestLayer1::onEnter()
{
    CCLOG("SceneTestLayer1#onEnter");
    CCLayer::onEnter();
}

void SceneTestLayer1::onEnterTransitionDidFinish()
{
    CCLOG("SceneTestLayer1#onEnterTransitionDidFinish");
    CCLayer::onEnterTransitionDidFinish();
}

SceneTestLayer1::~SceneTestLayer1()
{
}

void SceneTestLayer1::onPushScene(CCObject* pSender)
{
    //创建一个新的CCScene来作为当前的Scene
    CCScene* scene = new CCScene();
    CCLayer* pLayer = new SceneTestLayer2();
    scene->addChild( pLayer, 0 );
    CCDirector::sharedDirector()->pushScene( scene );
    scene->release();
    pLayer->release();
}

void SceneTestLayer1::onPushSceneTran(CCObject* pSender)
{
    //创建一个新的CCScene来作为当前的Scene
    CCScene* scene = new CCScene();
    CCLayer* pLayer = new SceneTestLayer2();
    scene->addChild( pLayer, 0 );
    //使用动画效果的切换
    CCDirector::sharedDirector()->pushScene( CCTransitionSlideInT::create(1, scene) );
    scene->release();
    pLayer->release();
}

//------------------------------------------------------------------
//
// SceneTestLayer2
//
//------------------------------------------------------------------

SceneTestLayer2::SceneTestLayer2()
{
    CCMenuItemFont* item = CCMenuItemFont::create( "Go Back", this, menu_selector(SceneTestLayer2::onGoBack) ); 
    CCMenu* menu = CCMenu::create( item, NULL );
    menu->alignItemsVertically(); 
    addChild( menu );
}

void SceneTestLayer2::onGoBack(CCObject* pSender)
{
    //移除
    CCDirector::sharedDirector()->popScene();
}

运行的效果:

  

posted on 2013-08-24 21:17  linzheng  阅读(713)  评论(0编辑  收藏  举报

导航