继续重建Agar.io的分割功能,它几乎可以工作,但只是在第二次......(以及之后)。 jsFiddle。片段:
Player.prototype.split():
split: function() {
if (this.radius < 100) return;
var len = this.cells.length;
this.radius /= 2;
this.cells.push(new Player(this.x, this.y, this.radius, this.speed, this.color, this.name, this.id + ' (split)'));
for (var i = 0; i < len; i++) {
this.cells[i].radius /= 2;
this.cells.push(new Player(this.cells[i].x, this.cells[i].y, this.cells[i].radius, this.cells[i].speed, this.cells[i].color, this.cells[i].name, this.cells[i].id + ' (split)'));
this.cells[i].accelerate();
for (var j = 0; j < this.cells[i].cells.length; i++) {
if (this.cells[i].cells[j] !== undefined) {
this.cells[i].cells[j].accelerate();
}
}
}
}
Player.prototype.accelerate():
accelerate: function() {
var self = this;
(function accelerate() {
self.x += self.accelX;
self.accelX *= 0.99;
requestAnimationFrame(accelerate);
}());
}
答案 0 :(得分:0)
您过早确定len
变量。
var len = this.cells.length;
...
this.cells.push(new Player(...));
for (var i = 0; i < len; i++) {
...
}
所以在第一次点击时永远不会输入循环。
我并不完全确定所需的行为,但你也可能过早地将初始半径减半(应该在推动之后,因为它在循环中又减半了一半?)。