PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)
背景:
? 本來打算把第三篇和第四篇合并都一起,但以前計劃分開,就還是分來吧;一般的游戲涉及到關(guān)卡的話,一般都會建立一個數(shù)組來存放各種定義參數(shù),消滅星星關(guān)卡比較容易,不需要建立數(shù)組,只有兩個參數(shù)level和target,而且這兩個參數(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; ?因為這樣的函數(shù)關(guān)系,你會發(fā)現(xiàn)越往后越難過關(guān),怪不得筆者一直達不到10000分;
ps:
1 CocosEditor已發(fā)布新版本,現(xiàn)在提供6個實戰(zhàn)demo學(xué)習(xí),包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;
2 代碼是基于javascript語言,cocos2d-x游戲引擎,cocos2d-x editor手游開發(fā)工具完成的;
3 運行demo需要配置好CocosEditor,暫不支持其他工具。demo是跨平臺的,可移植運行android,ios,html5網(wǎng)頁等。
源代碼下載:
請到代碼集中營下載(第三四篇合并 ?分?jǐn)?shù)和關(guān)卡):http://blog.makeapp.co/?p=319
不同平臺下的效果圖:(windows、html5、android)
windows
mac平臺
html5網(wǎng)頁
android平臺
代碼分析:
1 全局參數(shù),在主函數(shù)Main.js 如下定義當(dāng)前關(guān)卡和當(dāng)前關(guān)卡得到的分?jǐn)?shù);如果游戲沒有退出,兩個參數(shù)值一直保持不變,也可以通過這樣的方法在兩個場景之間傳遞值;
currentLevel = 1; ?
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;
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 游戲結(jié)束時,檢測是否勝利;
?如果勝利:下一個加1,currentLevel += 1; 下一關(guān)基礎(chǔ)分?jǐn)?shù)是這關(guān)的總分,currentLevelScore = this.totalScore; ?在MainLayer.js里面,筆者已經(jīng)定義過關(guān)卡精靈nextSprite,3秒后讓它顯示,里面還有一個移動動畫;7s后重新進入下一關(guān)MainLayer.js;
如果失敗:關(guān)卡和分?jǐn)?shù)都清空初始化,回到開始界面;
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是全球知名的游戲引擎 ,引擎在全球范圍內(nèi)擁有眾多開發(fā)者,涵蓋國內(nèi)外各知名游戲開發(fā)商。目前Cocos2d-x引擎已經(jīng)實現(xiàn)橫跨ios、Android、Bada、MeeGo、BlackBerry、Marmalade、Windows、Linux等平臺。編寫一次,到處運行,分為兩個版本 cocos2d-c++和cocos2d-html5 本文使用了后者;cocos2d-x 官網(wǎng):http://cocos2d-x.org/cocos2d-x 資料下載 ?http://cocos2d-x.org/download
CocosEditor開發(fā)工具:
CocosEditor,它是開發(fā)跨平臺的手機游戲工具,運行window/mac系統(tǒng)上,javascript腳本語言,基于cocos2d-x跨平臺游戲引擎, 集合代碼編輯,場景設(shè)計,動畫制作,字體設(shè)計,還有粒子,物理系統(tǒng),地圖等等的,而且調(diào)試方便,和實時模擬;
CocosEditor 下載,介紹和教程:http://blog.csdn.net/touchsnow/article/details/19070665;
CocosEditor官方博客:http://blog.makeapp.co/;
轉(zhuǎn)載于:https://blog.51cto.com/makeapp628/1398396
總結(jié)
以上是生活随笔為你收集整理的PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现在有一个整数数组,已知一个数出现的次数
- 下一篇: Spark 1.1.1 Submitti