您好,登錄后才能下訂單哦!
背景:
本來打算把第三篇和第四篇合并都一起,但以前計(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
mac平臺(tái)
html5網(wǎng)頁(yè)
android平臺(tái)
代碼分析:
1 全局參數(shù),在主函數(shù)Main.js 如下定義當(dāng)前關(guān)卡和當(dāng)前關(guān)卡得到的分?jǐn)?shù);如果游戲沒有退出,兩個(gè)參數(shù)值一直保持不變,也可以通過這樣的方法在兩個(gè)場(chǎng)景之間傳遞值;
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é)束時(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ù)都清空初始化,回到開始界面;
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 + "");
}
}
就這些,還是這么簡(jiǎn)單;:-D
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/;
免責(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)容。