先看一下类关系图,然后我们看关键类SceneTestLayer
SceneTestLayer1::SceneTestLayer1() { //建立3个菜单 CCMenuItemFont* item1 = CCMenuItemFont::create( "Test pushScene", this, menu_selector(SceneTestLayer1::onPushScene) ); CCMenuItemFont* item2 = CCMenuItemFont::create( "Test pushScene w/transition", this, menu_selector(SceneTestLayer1::onPushSceneTran) ); CCMenuItemFont* item3 = CCMenuItemFont::create( "Quit", this, menu_selector(SceneTestLayer1::onQuit) ); CCMenu* menu = CCMenu::create( item1, item2, item3, NULL ); //垂直对齐,水平对齐是 //menu->alignItemsHorizontally(); menu->alignItemsVertically(); addChild( menu ); //设置旋转的小人 CCSize s = CCDirector::sharedDirector()->getWinSize(); CCSprite* sprite = CCSprite::create(s_pPathGrossini); addChild(sprite); sprite->setPosition( ccp(s.width-40, s.height/2) ); CCActionInterval* rotate = CCRotateBy::create(2, 360); CCAction* repeat = CCRepeatForever::create(rotate); sprite->runAction(repeat); //不停调用testDealloc schedule( schedule_selector(SceneTestLayer1::testDealloc) ); }构造函数很简单,只是建立3个菜单,然后设置一个精灵,这个精灵不停旋转。我们在看看3个菜单具体做了什么。
void SceneTestLayer1::onPushScene(CCObject* pSender) { CCScene* scene = new SceneTestScene(); CCLayer* pLayer = new SceneTestLayer2(); scene->addChild( pLayer, 0 ); CCDirector::sharedDirector()->pushScene( scene ); scene->release(); pLayer->release(); } void SceneTestLayer1::onPushSceneTran(CCObject* pSender) { CCScene* scene = new SceneTestScene(); CCLayer* pLayer = new SceneTestLayer2(); scene->addChild( pLayer, 0 ); CCDirector::sharedDirector()->pushScene( CCTransitionSlideInT::create(1, scene) ); scene->release(); pLayer->release(); } void SceneTestLayer2::onReplaceScene(CCObject* pSender) { CCScene* pScene = new SceneTestScene(); CCLayer* pLayer = SceneTestLayer3::create(); pScene->addChild( pLayer, 0 ); CCDirector::sharedDirector()->replaceScene( pScene ); pScene->release(); } void SceneTestLayer2::onReplaceSceneTran(CCObject* pSender) { CCScene* pScene = new SceneTestScene(); CCLayer* pLayer = SceneTestLayer3::create(); pScene->addChild( pLayer, 0 ); CCDirector::sharedDirector()->replaceScene( CCTransitionFlipX::create(2, pScene) ); pScene->release(); }这里接触到2个新函数pushScene和2新类CCTransitionSlideInT,CCTransitionFlipX。pushScene对应的是popScene。接下来我们看SceneTestLayer3,这个类和前面2个类不一样。
class SceneTestLayer3 : public CCLayerColor { public: SceneTestLayer3(); bool init(); virtual void testDealloc(float dt); void item0Clicked(CCObject* pSender); void item1Clicked(CCObject* pSender); void item2Clicked(CCObject* pSender); CREATE_FUNC(SceneTestLayer3) } ;他重写了init和拥有create()函数,所以创建的时候直接create就行了,不需要管资源释放的问题。构造函数里面也不需要写什么,全部初始化的东西都放到init里面。
bool SceneTestLayer3::init() { if (CCLayerColor::initWithColor(ccc4(0,0,255,255))) { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCMenuItemFont *item0 = CCMenuItemFont::create("Touch to pushScene (self)", this, menu_selector(SceneTestLayer3::item0Clicked)); CCMenuItemFont *item1 = CCMenuItemFont::create("Touch to popScene", this, menu_selector(SceneTestLayer3::item1Clicked)); CCMenuItemFont *item2 = CCMenuItemFont::create("Touch to popToRootScene", this, menu_selector(SceneTestLayer3::item2Clicked)); CCMenu *menu = CCMenu::create(item0, item1, item2, NULL); this->addChild(menu); menu->alignItemsVertically(); this->schedule(schedule_selector(SceneTestLayer3::testDealloc)); CCSprite* sprite = CCSprite::create(s_pPathGrossini); addChild(sprite); sprite->setPosition( ccp(s.width/2, 40) ); CCActionInterval* rotate = CCRotateBy::create(2, 360); CCAction* repeat = CCRepeatForever::create(rotate); sprite->runAction(repeat); return true; } return false; }
void SceneTestLayer3::item0Clicked(CCObject* pSender) { CCScene *newScene = CCScene::create(); newScene->addChild(SceneTestLayer3::create()); CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(0.5, newScene, ccc3(0,255,255))); } void SceneTestLayer3::item1Clicked(CCObject* pSender) { CCDirector::sharedDirector()->popScene(); } void SceneTestLayer3::item2Clicked(CCObject* pSender) { CCDirector::sharedDirector()->popToRootScene(); }init里面没难看懂的东西。popScene()和popToRootScene()2个函数分别是只弹出一个和直接弹到在外面的区别。这节就看到这里,没特别难弄明白的东西。