【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十课:Cocos2D-X场景切换1-3

前端之家收集整理的这篇文章主要介绍了【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十课:Cocos2D-X场景切换1-3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【麦可网】Cocos2d-X跨平台游戏开发---学习笔记

第二十课:Cocos2D-X场景切换1-3

=======================================================================================================================================================================

课程目标:

-Cocos2D-X场景切换

课程重点:

-Cocos2D-X场景堆栈

-Cocos2D-X切换方式

考核目标:

-理解Cocos2D-X场景堆栈

-使用Cocos2D-X场景切换特效

=======================================================================================================================================================================

一、用图层切换画面

void MenuLayerMainMenu::menuCallback2(CCObject* sender)
{
    ((CCLayerMultiplex*)m_pParent)->switchTo(2);
}
其中:
void CCLayerMultiplex::switchTo(unsigned int n)
{
    CCAssert( n < m_pLayers->count(),"Invalid index in MultiplexLayer switchTo message" );

//删除图层
    this->removeChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer),true);

    m_nEnabledLayer = n;

//添加图层
    this->addChild((CCNode*)m_pLayers->objectAtIndex(n));
}


二、用场景切换画面

场景一:
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

	CCMenuItemLabel* cocos2dItem = CCMenuItemLabel::create( CCLabelTTF::create("Scence1","Arial",20),this,menu_selector(HelloWorld::menuCloseCallback) );
    cocos2dItem->setPosition(ccp(200,200));
	CCMenu* menu = CCMenu::createWithItem(cocos2dItem);
	menu->setPosition(CCPointZero);
	this->addChild(menu);

    return true;
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	CCScene* scence2 = SCENE2::scene();
	CCDirector::sharedDirector()->replaceScene(scence2);
}

------------------------------------------------------------------------
场景二:
bool SCENE2::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	CCMenuItemLabel* cocos2dItem = CCMenuItemLabel::create( CCLabelTTF::create("Scence2",menu_selector(SCENE2::menuCloseCallback) );
	cocos2dItem->setPosition(ccp(200,200));
	CCMenu* menu = CCMenu::createWithItem(cocos2dItem);
	menu->setPosition(CCPointZero);
	this->addChild(menu);
	
	return true;
}


void SCENE2::menuCloseCallback(CCObject* pSender)
{
	CCScene* scence1 = HelloWorld::scene();
	CCDirector::sharedDirector()->replaceScene(scence1);
}

ps:如果在包含头文件的时候出现:无法打开源文件。则表示头文件的位置放置不正确,应放在Class目录下。

切换场景的几个接口:
void CCDirector::replaceScene(CCScene *pScene)
{
    CCAssert(m_pRunningScene,"Use runWithScene: instead to start the director");
    CCAssert(pScene != NULL,"the scene should not be null");

    unsigned int index = m_pobScenesStack->count();

    m_bSendCleanupToScene = true;
    m_pobScenesStack->replaceObjectAtIndex(index - 1,pScene);

    m_pNextScene = pScene;
}

---------------------------------------------------------
void CCDirector::pushScene(CCScene *pScene)
{
    CCAssert(pScene,"the scene should not null");

    m_bSendCleanupToScene = false;

    m_pobScenesStack->addObject(pScene);
    m_pNextScene = pScene;
}

---------------------------------------------------------
void CCDirector::popScene(void)
{
    CCAssert(m_pRunningScene != NULL,"running scene should not null");

    m_pobScenesStack->removeLastObject();
    unsigned int c = m_pobScenesStack->count();

    if (c == 0)
    {
        end();
    }
    else
    {
        m_bSendCleanupToScene = true;
        m_pNextScene = (CCScene*)m_pobScenesStack->objectAtIndex(c - 1);
    }
}

---------------------------------------------------------
void CCDirector::popToRootScene(void)
{
    popToSceneStackLevel(1);
}

给切换的场景添加动画:
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	CCScene* scence2 = SCENE2::scene();
	CCScene* tra_scence2 = CCTransitionSlideInB::create(2.0,scence2);
	CCDirector::sharedDirector()->replaceScene(tra_scence2);
}

---------------------------------------------------------------
void SCENE2::menuCloseCallback(CCObject* pSender)
{
	CCScene* scence1 = HelloWorld::scene();
	CCScene* tra_scence1 = CCTransitionMoveInL::create(2.0f,scence1);
	CCDirector::sharedDirector()->replaceScene(tra_scence1);
}

===================================================================

总结:

好的切换场景动画,能够提高用户体验。

开心一刻:

何四与邻居发生争执,何四便粗鲁地骂对方:“你是猪!”此事被居民小区治安员知道了,于是要罚何四30元款。

何四接过罚单,很不服气:“上个月我也是骂他是猪,你只罚了我20元嘛。”

“很抱歉,”治安员苦笑一声,“近段猪肉涨价了。”

【麦可网】Cocos2d-X跨平台游戏开发---教程下载:http://pan.baidu.com/s/1kTio1Av

【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu

原文链接:https://www.f2er.com/cocos2dx/342453.html

猜你在找的Cocos2d-x相关文章