背景:
这一篇是结尾篇,主要分析地雷检测,游戏结束和保存最高分;
ps:
1 CocosEditor已发布新版本,现在提供6个实战demo学习,包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;
2 代码是基于javascript语言,cocos2d-x游戏引擎,CocosEditor手游开发工具完成的;
3 运行demo需要配置好
CocosEditor
,暂不支持其他工具。demo是跨平台的,可移植运行android,ios,html5网页等。
源代码下载:
请到代码集中营下载(
水果忍者
):
http://blog.makeapp.co/?p=319
效果图:
代码分析:
1 进入主场景从本地数据库中获取最高分,并显示最高分文字this.bestScoreLabel;
-
//bestScore
-
this
.bestScore = sys.localStorage.getItem(
“bestScore”
);
-
if
(
this
.bestScore !=
null
&&
this
.bestScore != undefined) {
-
this
.bestScore = Number(
this
.bestScore);
-
}
-
else
{
-
this
.bestScore = 0;
-
}
-
cc.log(
“bestScore==”
+
this
.bestScore);
-
this
.bestScoreLabel.setString(FRUIT_STRINGS.bestScore +
this
.bestScore);
-
this
.overLayer.setVisible(
false
);
2 在触摸移动的过程中,会切到水果也会切刀地雷,如果是地雷,游戏直接结束 ;
#我们已经建立过水果数组,地雷的编号num是5,所以很简单,只要判断水果的编号就可以轻易的确定地雷;
#如果是地雷,游戏状态over,播放音效boom;
#创建地雷光芒light精灵;
#播放一个序列动画,放大选择 然后清除,最后回调到结束函数this.gameOver();
-
//if bomb
-
if
(fruit.num == 5) {
-
this
.gameStatus = OVER;
-
cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.boom,
false
);
-
var
light = cc.MySprite.create(
this
.rootNode, FRUIT_DATA[5].cutImage, loc, 1100);
-
light.runAction(cc.Sequence.create(
-
cc.Spawn.create(cc.ScaleTo.create(2, 10), cc.RotateBy.create(1, 360)),
-
cc.CleanUp.create(light),
-
cc.CallFunc.create(
function
()
-
{
-
this
.gameOver();
-
},
this
)
-
));
-
return
;
-
}
3 游戏结束有两种情况,一种是时间到,另一种是切到地雷;
#游戏结束后,显示游戏层overLayer;
#提示你得到的分数gameScoreTip;
#如果本次玩的最高分大于历史最佳分数,本地数据库存储当前分数
#延时6s回到开始界面;
-
MainLayer.prototype.gameOver =
function
()
-
{
-
cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.over,
false
);
-
this
.overLayer.setZOrder(1000);
-
this
.overLayer.setVisible(
true
);
-
-
var
gameScoreTip = FRUIT_STRINGS.youGet +
this
.totalScore + FRUIT_STRINGS.score;
-
cc.log(
“this.totalScore=”
+
this
.totalScore);
-
cc.log(
“this.bestScore=”
+
this
.bestScore);
-
if
(
this
.totalScore >
this
.bestScore) {
-
cc.log(
“this.bestScore=”
+
this
.bestScore);
-
sys.localStorage.setItem(
“bestScore”
,
this
.totalScore +
“”
);
-
gameScoreTip = gameScoreTip + FRUIT_STRINGS.record;
-
}
-
this
.overScoreLabel.setString(gameScoreTip);
-
-
this
.overLayer.scheduleOnce(
function
()
-
{
-
cc.AudioEngine.getInstance().stopAllEffects();
-
cc.BuilderReader.runScene(
“”
,
“StartLayer”
);
-
}, 6);
-
-
};
水果忍者系列结束