Cocos2d-x 游戲場(chǎng)景過(guò)渡

2018-10-02 09:56 更新

游戲場(chǎng)景過(guò)渡

游戲是實(shí)現(xiàn)了,但是如果有個(gè)歡迎界面和一個(gè)結(jié)束界面就更好了。

歡迎界面可以用于預(yù)加載一些資源,初始化數(shù)據(jù),顯示logo,copyright之類(lèi)的東西,而結(jié)束畫(huà)面用于顯示游戲得分,以及返回游戲,退出游戲等等。

1.歡迎界面

本系列到這里,歡迎場(chǎng)景的建立應(yīng)該不用多說(shuō)了。還是一樣的WelcomeLayer和WelcomeScene。

WelcomeLayer的init主要實(shí)現(xiàn)的功能有:

1.1.將plist載入全局cache中

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/shoot_background.plist");
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/shoot.plist");

1.2.加入copyright和loading動(dòng)畫(huà)

    //加入copyright
    ...

    //加入loading
    CCSprite* loading=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_loading1.png"));
    loading->setPosition(ccp(winSize.width/2,winSize.height/2-40));
    this->addChild(loading);

    CCAnimation* animation=CCAnimation::create();//幀動(dòng)畫(huà)
    animation->setDelayPerUnit(0.2f);
    animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_loading1.png"));
    animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_loading2.png"));
    animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_loading3.png"));
    animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_loading4.png"));

    CCAnimate* animate=CCAnimate::create(animation);
    CCRepeat* repeat=CCRepeat::create(animate,2);//重復(fù)2次
    CCCallFuncN* repeatdone=CCCallFuncN::create(this,callfuncN_selector(WelcomeLayer::loadingDone));//loadingDone用來(lái)轉(zhuǎn)換場(chǎng)景
    CCSequence* sequence=CCSequence::create(repeat,repeatdone);
    loading->runAction(sequence);

1.3.加載歷史最高分

    getHighestHistorySorce();

2.結(jié)束畫(huà)面

(1)GameOverLayer是結(jié)束畫(huà)面,它比較特殊的一點(diǎn)是create,我們給它傳個(gè)參數(shù),這個(gè)參數(shù)就是在游戲場(chǎng)景切換到結(jié)束場(chǎng)景傳遞的分?jǐn)?shù)值。

    GameOverLayer* GameOverLayer::create(int passScore)
    {
        GameOverLayer *pRet = new GameOverLayer();
        pRet->score=passScore;//score是成員變量
        if (pRet && pRet->init())
        {
            pRet->autorelease();
            return pRet;
        }
        else
        {
            delete pRet;
            pRet = NULL;
            return NULL;
        }
    }

(2)中間顯示本局分?jǐn)?shù),左上角顯示歷史最高分,當(dāng)切換到結(jié)束畫(huà)面時(shí),分?jǐn)?shù)做一個(gè)放大縮小的動(dòng)畫(huà)。

    CCDelayTime* delay=CCDelayTime::create(1.0f);
    CCScaleTo* scalebig=CCScaleTo::create(1.0f,3.0f);
    CCScaleTo* scalelittle=CCScaleTo::create(0.3f,2.0f);
    CCFiniteTimeAction* sequence=CCSequence::create(delay,scalebig,scalelittle,NULL);
    finalScore->runAction(sequence);

(3)如果當(dāng)前分?jǐn)?shù)破紀(jì)錄,左上角歷史最高分要進(jìn)行更新。

    if (score>highestHistoryScore)
    {
        CCUserDefault::sharedUserDefault()->setIntegerForKey("HighestScore",score);
        highestHistoryScore=score;
        CCDelayTime* delayChange=CCDelayTime::create(1.3f);
        CCMoveBy* moveOut=CCMoveBy::create(0.1f,ccp(0,100));
        CCCallFuncN* beginChange=CCCallFuncN::create(this,callfuncN_selector(GameOverLayer::beginChangeHighestScore));
        CCMoveBy* moveIn=CCMoveBy::create(0.1f,ccp(0,-100));
        CCFiniteTimeAction* sequence=CCSequence::create(delayChange,moveOut,beginChange,moveIn,NULL);
        highestScore->runAction(sequence);
    }

(4)右下角有返回游戲功能CCMenu,點(diǎn)擊后進(jìn)行場(chǎng)景切換。

3.場(chǎng)景切換

3.1.場(chǎng)景切換

這個(gè)游戲總共有3個(gè)場(chǎng)景。歡迎界面,游戲界面和游戲結(jié)束界面。它們的關(guān)系是:

    WelcomeScene→GameScene?GameOverScene

場(chǎng)景的切換我們使用

    CCDirector::sharedDirector()->replaceScene(CCScene* pSecne);//相關(guān)資源會(huì)進(jìn)行銷(xiāo)毀

關(guān)于popScene和pushScene的使用,有興趣的可以自己百度。

3.2.場(chǎng)景切換效果

在cocos2d-x的示例中有這么多的場(chǎng)景過(guò)渡效果。當(dāng)然它們的使用都是一致的,把下一個(gè)場(chǎng)景指針傳入create中獲得一個(gè)帶動(dòng)畫(huà)效果的新場(chǎng)景指針,進(jìn)行切換即可。喜歡什么樣的效果,自己去挑吧。

    "CCTransitionJumpZoom",

    "CCTransitionProgressRadialCCW",
    "CCTransitionProgressRadialCW",
    "CCTransitionProgressHorizontal",
    "CCTransitionProgressVertical",
    "CCTransitionProgressInOut",
    "CCTransitionProgressOutIn",

    "CCTransitionCrossFade",
    "TransitionPageForward",
    "TransitionPageBackward",
    "CCTransitionFadeTR",
    "CCTransitionFadeBL",
    "CCTransitionFadeUp",
    "CCTransitionFadeDown",
    "CCTransitionTurnOffTiles",
    "CCTransitionSplitRows",
    "CCTransitionSplitCols",

    "CCTransitionFade",
    "FadeWhiteTransition",

    "FlipXLeftOver",
    "FlipXRightOver",
    "FlipYUpOver",
    "FlipYDownOver",
    "FlipAngularLeftOver",
    "FlipAngularRightOver",

    "ZoomFlipXLeftOver",
    "ZoomFlipXRightOver",
    "ZoomFlipYUpOver",
    "ZoomFlipYDownOver",
    "ZoomFlipAngularLeftOver",
    "ZoomFlipAngularRightOver",

    "CCTransitionShrinkGrow",
    "CCTransitionRotoZoom",

    "CCTransitionMoveInL",
    "CCTransitionMoveInR",
    "CCTransitionMoveInT",
    "CCTransitionMoveInB",
    "CCTransitionSlideInL",
    "CCTransitionSlideInR",
    "CCTransitionSlideInT",
    "CCTransitionSlideInB",

4.游戲退出

在win32調(diào)試的時(shí)候沒(méi)注意到這個(gè)問(wèn)題,但是當(dāng)移植到安卓的時(shí)候才發(fā)現(xiàn),按Back鍵竟然木有用,而Home鍵只能切換到主屏,也不能退出游戲。

還記得CCLayer從哪里繼承來(lái)的么?我們?cè)倏匆槐椋?/p>

    CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate

最后的CCKeypadDelegate就是解決這個(gè)問(wèn)題的方法。

在各個(gè)主場(chǎng)景的主層中重載

    virtual void keyBackClicked();//對(duì)應(yīng)back鍵
    virtual void keyMenuClicked();//對(duì)應(yīng)home鍵

在init中調(diào)用

    this->setKeypadEnabled(true);

keyBackClicked()的實(shí)現(xiàn)

    void GameOverLayer::keyBackClicked()
    {
        CCDirector::sharedDirector()->end();//結(jié)束游戲
    }

這一節(jié)感覺(jué)有點(diǎn)多啊。。。就這樣吧。

效果圖



以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)