我一直在try 用Java脚本创建一个简单的粒子emits 器.但当编写删除超过生命周期 的粒子的函数时,粒子的添加速度会更快.以下是我的代码:
this.addParticle = function() {
var p = new Particle(
random(this.x - 10, this.x + 10),
random(this.y - 10, this.y + 10),
random(this.xSpeed - 1, this.xSpeed + 1),
random(this.ySpeed - 1, this.ySpeed + 1),
random(this.size - 4, this.size + 4),
this.colour
);
this.particles.push(p);
return p;
}
this.updateParticles = function() {
//iterate through particles and draw to screen
for(let i = this.particles.length-1; i >= 0; i--) {
this.particles[i].drawParticle();
this.particles[i].updateParticle();
if (this.particles[i].age > this.lifetime) {
this.particles.splice(i, 1); // removes particle
this.particles.push(this.addParticle()); // adds new particle
}
}
}
我知道,不知何故,更多的粒子被推入数组,这可以通过简单地判断if (this.particles.length < this.startParticles)
个来解决.然而,我不能理解为什么在我的更新粒子函数中添加粒子的速度可能比删除粒子的速度快-粒子不是按1对1的方式添加的吗?