目前我有一款游戏,当你点击画布时,它上面会画一块食物。然后鱼精灵会朝着食物的方向吃掉它。
这是让鱼走向食物的功能:
Fish.prototype.eatFood = function() {
var foodX, foodY;
var halfWidth = this.frameWidth * this.frameScale / 2;
var halfHeight = this.frameHeight * this.frameScale / 2;
// Loop backward because we are removing elements.
for (var i = foodArray.length-1; i >= 0; i--) {
foodX = foodArray[i].x + foodWidth / 2;
foodY = foodArray[i].y + foodHeight / 2;
if (foodX > this.xPos - halfWidth &&
foodX < this.xPos + halfWidth &&
foodY > this.yPos - halfHeight&&
foodY < this.yPos + halfHeight)
{
foodArray.splice(i, 1); //Clearing away food after both of it collide
}
}
};
我怎样才能设置一个计数器,所以每当鱼吃食物时,计数器就会增加。
答案 0 :(得分:0)
这可能会有所帮助:在脚本开头创建变量(全局)并将其初始化为0
。然后每当食物消失时,就在这里:{
foodArray.splice(i, 1); //Clearing away food after both of it collide
}
增加变量。像这样:
` {
foodArray.splice(i, 1); //Clearing away food after both of it collide
foodCount++;
} `
这样,每次鱼吃食物都会增加。