溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)

發(fā)布時(shí)間:2020-03-03 16:17:52 來源:網(wǎng)絡(luò) 閱讀:569 作者:makeapp628 欄目:移動(dòng)開發(fā)

背景:

  本來打算把第三篇和第四篇合并都一起,但以前計(jì)劃分開,就還是分來吧;一般的游戲涉及到關(guān)卡的話,一般都會(huì)建立一個(gè)數(shù)組來存放各種定義參數(shù),消滅星星關(guān)卡比較容易,不需要建立數(shù)組,只有兩個(gè)參數(shù)level和target,而且這兩個(gè)參數(shù)還存在函數(shù)關(guān)系:target=1000*(level+1)*level/2,只要知道第幾關(guān)就可以得到該關(guān)的目標(biāo)分?jǐn)?shù),比如第三關(guān),目標(biāo)分?jǐn)?shù)就是 1000*(3+1)*3/2=6000;  因?yàn)檫@樣的函數(shù)關(guān)系,你會(huì)發(fā)現(xiàn)越往后越難過關(guān),怪不得筆者一直達(dá)不到10000分;

ps:

1 CocosEditor已發(fā)布新版本,現(xiàn)在提供6個(gè)實(shí)戰(zhàn)demo學(xué)習(xí),包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;

2 代碼是基于javascript語言,cocos2d-x游戲引擎,cocos2d-x editor手游開發(fā)工具完成的;

3 運(yùn)行demo需要配置好CocosEditor,暫不支持其他工具。demo是跨平臺(tái)的,可移植運(yùn)行android,ios,html5網(wǎng)頁(yè)等。



源代碼下載:

請(qǐng)到代碼集中營(yíng)下載(第三四篇合并  分?jǐn)?shù)和關(guān)卡):http://blog.makeapp.co/?p=319



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


windows

PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)



mac平臺(tái)

PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)


html5網(wǎng)頁(yè)

PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)


android平臺(tái)


PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)


代碼分析:


1 全局參數(shù),在主函數(shù)Main.js 如下定義當(dāng)前關(guān)卡和當(dāng)前關(guān)卡得到的分?jǐn)?shù);如果游戲沒有退出,兩個(gè)參數(shù)值一直保持不變,也可以通過這樣的方法在兩個(gè)場(chǎng)景之間傳遞值;


[javascript]view plaincopyPopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)
  1. currentLevel = 1;  

  2. currentLevelScore = 0;  




2 MainLayer.js里面onEnter函數(shù)初始化,當(dāng)前關(guān)卡和目標(biāo)分?jǐn)?shù),獲得的總分;目標(biāo)分?jǐn)?shù)就是上面說的函數(shù) this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;


[javascript]view plaincopyPopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)
  1. MainLayer.prototype.onEnter = function ()  

  2. {  

  3.    cc.log("onEnter");  

  4. this.pauseNode.setZOrder(120);  

  5. //init stars

  6. this.initStarTable();  

  7. //stage

  8. this.stageFont.setString(currentLevel + "");  

  9. //target  score

  10. this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;  

  11. this.targetFont.setString(this.targetScore + "");  

  12. //score

  13. this.totalScore = currentLevelScore;  

  14. this.scoreFont.setString(this.totalScore + "");  

  15. //score tip

  16. this.scoreTipLabel.setVisible(false);  

  17. this.tipLabel.setVisible(false);  

  18. this.tipLabel.setZOrder(10);  

  19. //best score

  20. this.bestScore = sys.localStorage.getItem("starBestScore");  

  21. if (this.bestScore != null && this.bestScore != undefined) {  

  22. this.bestScore = Number(this.bestScore);  

  23.    }  

  24. else {  

  25. this.bestScore = 0;  

  26.    }  

  27. this.bestScoreFont.setString(this.bestScore + "");  

  28. }  




3 游戲結(jié)束時(shí),檢測(cè)是否勝利;

 如果勝利:下一個(gè)加1,currentLevel += 1; 下一關(guān)基礎(chǔ)分?jǐn)?shù)是這關(guān)的總分,currentLevelScore = this.totalScore;  在MainLayer.js里面,筆者已經(jīng)定義過關(guān)卡精靈nextSprite,3秒后讓它顯示,里面還有一個(gè)移動(dòng)動(dòng)畫;7s后重新進(jìn)入下一關(guān)MainLayer.js;

如果失敗:關(guān)卡和分?jǐn)?shù)都清空初始化,回到開始界面;


[javascript]view plaincopyPopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)PopStar(消滅星星)游戲源代碼下載、分析及跨平臺(tái)移植---第四篇(關(guān)卡)
  1. MainLayer.prototype.winStar = function ()  

  2. {  

  3. if (this.isClear == true) {  

  4.        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.win);  

  5.        cc.Toast.create(this.rootNode, "Win", 3);  

  6.        currentLevel += 1;  

  7.        currentLevelScore = this.totalScore;  

  8. this.nextSprite.setZOrder(100);  

  9. var that = this;  

  10. this.rootNode.scheduleOnce(function ()  

  11.        {  

  12.            that.nextLevelLabel.setString("level " + currentLevel + "");  

  13.            that.nextTargetLabel.setString("target " + 1000 * (1 + currentLevel) * currentLevel / 2);  

  14.            that.nextSprite.runAction(cc.Sequence.create(  

  15.                    cc.MoveTo.create(1, cc.p(0, 0)),  

  16.                    cc.DelayTime.create(2),  

  17.                    cc.MoveTo.create(1, cc.p(-730, 0))  

  18.            ))  

  19.        }, 3);  

  20. this.rootNode.scheduleOnce(function ()  

  21.        {  

  22.            cc.BuilderReader.runScene("", "MainLayer");  

  23.        }, 7);  

  24.    }  

  25. else {  

  26.        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.gameover);  

  27.        currentLevel = 1;  

  28.        currentLevelScore = 0;  

  29.        cc.Toast.create(this.rootNode, "lost", 2);  

  30. this.rootNode.scheduleOnce(function ()  

  31.        {  

  32.            cc.BuilderReader.runScene("", "StartLayer");  

  33.        }, 2)  

  34.    }  

  35. if (this.totalScore > this.bestScore) {  

  36.        sys.localStorage.setItem("starBestScore", this.totalScore + "");  

  37.    }  

  38. }  


就這些,還是這么簡(jiǎn)單;:-D



cocos2d-x跨平臺(tái)游戲引擎

cocos2d-x是全球知名的游戲引擎 ,引擎在全球范圍內(nèi)擁有眾多開發(fā)者,涵蓋國(guó)內(nèi)外各知名游戲開發(fā)商。目前Cocos2d-x引擎已經(jīng)實(shí)現(xiàn)橫跨ios、Android、Bada、MeeGo、BlackBerry、Marmalade、Windows、Linux等平臺(tái)。編寫一次,到處運(yùn)行,分為兩個(gè)版本 cocos2d-c++和cocos2d-html5 本文使用了后者;
cocos2d-x 官網(wǎng):http://cocos2d-x.org/
cocos2d-x 資料下載  http://cocos2d-x.org/download




CocosEditor開發(fā)工具:

CocosEditor,它是開發(fā)跨平臺(tái)的手機(jī)游戲工具,運(yùn)行window/mac系統(tǒng)上,javascript腳本語言,基于cocos2d-x跨平臺(tái)游戲引擎, 集合代碼編輯,場(chǎng)景設(shè)計(jì),動(dòng)畫制作,字體設(shè)計(jì),還有粒子,物理系統(tǒng),地圖等等的,而且調(diào)試方便,和實(shí)時(shí)模擬;

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

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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI