PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第四篇(關卡)

背景:

   本來打算把第三篇和第四篇合併都一起,但以前計劃分開,就還是分來吧;一般的遊戲涉及到關卡的話,一般都會建立一個數組來存放各種定義參數,消滅星星關卡比較容易,不需要建立數組,只有兩個參數level和target,而且這兩個參數還存在函數關係:target=1000*(level+1)*level/2,只要知道第幾關就可以得到該關的目標分數,比如第三關,目標分數就是 1000*(3+1)*3/2=6000;  因爲這樣的函數關係,你會發現越往後越難過關,怪不得筆者一直達不到10000分;

ps:

1 CocosEditor已發佈新版本,現在提供6個實戰demo學習,包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;

2 代碼是基於javascript語言,cocos2d-x遊戲引擎,cocos2d-x editor手遊開發工具完成的;

3 運行demo需要配置好CocosEditor,暫不支持其他工具。demo是跨平臺的,可移植運行android,ios,html5網頁等。


源代碼下載:

請到代碼集中營下載(第三四篇合併  分數和關卡):http://blog.makeapp.co/?p=319


不同平臺下的效果圖:(windows、html5、android)


windows



mac平臺


html5網頁



android平臺


          


代碼分析:


1 全局參數,在主函數Main.js 如下定義當前關卡和當前關卡得到的分數;如果遊戲沒有退出,兩個參數值一直保持不變,也可以通過這樣的方法在兩個場景之間傳遞值;

currentLevel = 1;
currentLevelScore = 0;


2 MainLayer.js裏面onEnter函數初始化,當前關卡和目標分數,獲得的總分;目標分數就是上面說的函數 this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;

MainLayer.prototype.onEnter = function ()
{
    cc.log("onEnter");
    this.pauseNode.setZOrder(120);

    //init stars
    this.initStarTable();

    //stage
    this.stageFont.setString(currentLevel + "");

    //target  score
    this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;
    this.targetFont.setString(this.targetScore + "");

    //score
    this.totalScore = currentLevelScore;
    this.scoreFont.setString(this.totalScore + "");

    //score tip
    this.scoreTipLabel.setVisible(false);
    this.tipLabel.setVisible(false);
    this.tipLabel.setZOrder(10);

    //best score
    this.bestScore = sys.localStorage.getItem("starBestScore");
    if (this.bestScore != null && this.bestScore != undefined) {
        this.bestScore = Number(this.bestScore);
    }
    else {
        this.bestScore = 0;
    }
    this.bestScoreFont.setString(this.bestScore + "");
}


3 遊戲結束時,檢測是否勝利;

  如果勝利:下一個加1,currentLevel += 1; 下一關基礎分數是這關的總分,currentLevelScore = this.totalScore;  在MainLayer.js裏面,筆者已經定義過關卡精靈nextSprite,3秒後讓它顯示,裏面還有一個移動動畫;7s後重新進入下一關MainLayer.js;

 如果失敗:關卡和分數都清空初始化,回到開始界面;

MainLayer.prototype.winStar = function ()
{
    if (this.isClear == true) {
        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.win);
        cc.Toast.create(this.rootNode, "Win", 3);
        currentLevel += 1;
        currentLevelScore = this.totalScore;

        this.nextSprite.setZOrder(100);
        var that = this;
        this.rootNode.scheduleOnce(function ()
        {
            that.nextLevelLabel.setString("level " + currentLevel + "");
            that.nextTargetLabel.setString("target " + 1000 * (1 + currentLevel) * currentLevel / 2);
            that.nextSprite.runAction(cc.Sequence.create(
                    cc.MoveTo.create(1, cc.p(0, 0)),
                    cc.DelayTime.create(2),
                    cc.MoveTo.create(1, cc.p(-730, 0))
            ))
        }, 3);
        this.rootNode.scheduleOnce(function ()
        {
            cc.BuilderReader.runScene("", "MainLayer");
        }, 7);
    }
    else {
        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.gameover);
        currentLevel = 1;
        currentLevelScore = 0;
        cc.Toast.create(this.rootNode, "lost", 2);
        this.rootNode.scheduleOnce(function ()
        {
            cc.BuilderReader.runScene("", "StartLayer");
        }, 2)
    }
    if (this.totalScore > this.bestScore) {
        sys.localStorage.setItem("starBestScore", this.totalScore + "");
    }
}

就這些,還是這麼簡單;:-D



cocos2d-x跨平臺遊戲引擎

cocos2d-x是全球知名的遊戲引擎 ,引擎在全球範圍內擁有衆多開發者,涵蓋國內外各知名遊戲開發商。目前Cocos2d-x引擎已經實現橫跨ios、Android、Bada、MeeGo、BlackBerry、Marmalade、Windows、Linux等平臺。編寫一次,到處運行,分爲兩個版本 cocos2d-c++和cocos2d-html5 本文使用了後者;
cocos2d-x 官網:http://cocos2d-x.org/
cocos2d-x 資料下載  http://cocos2d-x.org/download


CocosEditor開發工具:

CocosEditor,它是開發跨平臺的手機遊戲工具,運行window/mac系統上,javascript腳本語言,基於cocos2d-x跨平臺遊戲引擎, 集合代碼編輯,場景設計,動畫製作,字體設計,還有粒子,物理系統,地圖等等的,而且調試方便,和實時模擬;

CocosEditor 下載,介紹和教程:http://blog.csdn.net/touchsnow/article/details/19070665

CocosEditor官方博客:http://blog.makeapp.co/



PopStar博文系列:

PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第一篇(界面) 

PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第二篇(算法) 

PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第三篇(分數)  

PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第四篇(關卡) 

PopStar(消滅星星)遊戲源代碼下載、分析及跨平臺移植---第五篇(移植) 



傳送門(優質博文):

flappy bird遊戲源代碼揭祕和下載

flappy bird遊戲源代碼揭祕和下載後續---移植到android真機上

flappy bird遊戲源代碼揭祕和下載後續---移植到html5網頁瀏覽器

flappy bird遊戲源代碼揭祕和下載後續---日進5萬美元的祕訣AdMob廣告 


筆者語:

想了解更多請進入官方博客,最新博客和代碼在官方博客首發;請持續關注,還有更多cocos2dx editor遊戲源碼即將放出;

聯繫筆者:[email protected](郵箱) qq羣:232361142


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章