背景:
经过消灭星星第二篇算法,最高的山峰已经过去了,剩下的都是小沟小河,没什么难度了。这一节笔者继续完成消灭星星的分数篇,这节主要包括:触摸提示得分 比如4 blocks 80 points,然后产生单个星星分数并移动到总分上,最后保存最高分;
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)
window平台
mac平台
html5网页平台
android平台
代码分析:
1 进入MainLayer.js在onEnter函数里面,我们需要获取数据库里面的最高分,在cocos里面我们一般通过sys.localStorage.getItem 和sys.localStorage.setItem来获取和存储简单的数值;
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 + "");
2 定义分数规则
我们先定义分数规则,选择的星星越多,分数越高,所以规则是一个二次函数 score=length*length*5;比如选择4个星星 分数就是4*4*5=80,如果选择10个星星 分数就是10*10*5=500;然后触摸星星的时候,如果相同星星的数量大于1时 提示获取的分数
if (this.sameColorList.length > 1) {
cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.select, false);
this.showScoreTip();
}
else {
this.scoreTipLabel.setVisible(false);}
MainLayer.prototype.showScoreTip = function ()
{
this.scoreTipLabel.setVisible(true);
var length = this.sameColorList.length;
var tip = length + " blocks " + length * length * 5 + " points";
this.scoreTipLabel.setString(tip);
}
var starScoreSprite = cc.StarLabel.createScore(this.rootNode, cc.p((36 + col * this.starSize), (36 + row * this.starSize)), this.oneStarScore + "");
starScoreSprite.runAction(cc.Sequence.create(
cc.MoveTo.create(0.3 + k / 20, this.scoreFont.getPosition()),
cc.CleanUp.create(starScoreSprite),
cc.CallFunc.create(function ()
{
this.totalScore += this.oneStarScore;
this.scoreFont.setString(this.totalScore + "");
if (this.totalScore >= this.targetScore) {
if (this.isClear == false) {
this.isClear = true;
this.tipLabel.setVisible(true);
this.tipLabel.setString("Clear!");
this.tipLabel.runAction(cc.Sequence.create(cc.DelayTime.create(1),
cc.MoveTo.create(1, cc.p(110, 1000))
));
cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.stageclear);
}
}
}, this)
));
if (deadCount < 10) {
if (deadCount == 0) {
this.totalScore += 1000;
this.scoreFont.setString(this.totalScore + "");
}
this.oneDeadStarScore = Math.floor((1000 - deadCount * 100) / deadCount);
this.oneDeadStarScore = this.oneDeadStarScore - this.oneDeadStarScore % 10;
var starScoreSprite = cc.StarLabel.createScore(this.rootNode,
cc.p((36 + ii * this.starSize), (36 + jj * this.starSize)), this.oneDeadStarScore + "");
starScoreSprite.runAction(cc.Sequence.create(
cc.ScaleTo.create(0, 0),
cc.DelayTime.create(delay), cc.ScaleTo.create(0, 1),
cc.MoveTo.create(0.3 + jj / 20, this.scoreFont.getPosition()),
cc.CleanUp.create(starScoreSprite),
cc.CallFunc.create(function ()
{
this.totalScore += this.oneDeadStarScore;
this.scoreFont.setString(this.totalScore + "");
}, this)
));
}
if (this.totalScore > this.bestScore) {
sys.localStorage.setItem("starBestScore", this.totalScore + "");
}
cc.CleanUp = {};
cc.CleanUp.create = function (sprite)
{
return cc.CallFunc.create(function ()
{
sprite.cleanuped = true;
sprite.removeFromParent(true);
});
}
cc.StarParticle = {};
cc.StarParticle.create = function (node, x, y, name)
{
var particle = cc.ParticleSystem.create("Resources/particles/" + name + ".plist");
particle.setAnchorPoint(cc.p(0.5, 0.5));
particle.setPosition(cc.p(x, y));
particle.setZOrder(120);
node.addChild(particle);
return particle;
};
cc.StarLabel = {};
cc.StarLabel.createScore = function (node, p, message)
{
var label = cc.LabelTTF.create(message, "Arial", 30);
label.setPosition(p);
label.setColor(cc.c3b(255, 255, 255));
label.setZOrder(10000);
node.addChild(label);
return label;
};
cc.StarLabel.createTip = function (node, p, message)
{
var label = cc.LabelTTF.create(message, "Arial", 30);
label.setPosition(p);
label.setColor(cc.c3b(255, 255, 255));
label.setZOrder(10000);
node.addChild(label);
return label;
};
cc.Toast = {};
cc.Toast.create = function (node, message, delay)
{
cc.log("toast");
var director = cc.Director.getInstance();
var size = director.getWinSize();
var label = cc.LabelTTF.create(message, "Arial", 40);
label.setPosition(size.width / 2, size.height / 2 + 100);
label.setColor(cc.c3b(255, 255, 255));
label.setZOrder(10000);
node.addChild(label);
label.runAction(cc.Sequence.create(cc.DelayTime.create(delay), cc.CleanUp.create(label)));
return label;
};
cc.rectCreate = function (p, area)
{
return cc.rect(p.x - area[0], p.y - area[1], area[0] * 2, area[1] * 2);
}
function getRandom(maxSize)
{
return Math.floor(Math.random() * maxSize) % maxSize;
}
很简单,笔者感觉没写什么。。。
CocosEditor开发工具:
CocosEditor,它是开发跨平台的手机游戏工具,运行window/mac系统上,javascript脚本语言,基于cocos2d-x跨平台游戏引擎, 集合代码编辑,场景设计,动画制作,字体设计,还有粒子,物理系统,地图等等的,而且调试方便,和实时模拟;
CocosEditor 下载,介绍和教程:http://blog.csdn.net/touchsnow/article/details/19070665;
CocosEditor官方博客:http://blog.makeapp.co/;