I am learing gamedev using HTML 5 Canvas and while writing my second game I got strange problem. Long story short: I create particles based on enemy size when projectile and enemy collide and than I want to render those particles on the canvas. Sadly even though the number of particles being pushed to the array is correct, I'm only getting one particle rendered on the screen. Can u provide any ideas or solutions for this?
particles.forEach((particle, idx) => {
if (particle.alpha <= 0) particles.splice(idx, 1);
else particle.update()
});
And the update method that is rendering particles is:
draw() {
c.save();
c.globalAlpha = this._alpha;
c.beginPath();
c.arc(this._position.x, this._position.y, this._radius, 0, Math.PI * 2, false);
c.fillStyle = this._color;
c.fill();
c.restore();
}
update() {
this.draw();
this._velocity.x *= friction;
this._velocity.y *= friction;
this._position.x += this._velocity.x;
this._position.y += this._velocity.y;
this._alpha -= 0.01;
}
Lastly, the code to actually push new particle to the array is:
const createParticles = (enemy: Enemy, projectile: Projectile) => {
for (let i = 0; i < enemy.radius * 2; i++) {
particles.push(new Particle({ position: projectile.position, radius: Math.random() * 2, color: enemy.color, velocity: {
x: (Math.random() - 0.5) * (Math.random() * 2),
y: (Math.random() - 0.5) * (Math.random() * 2)
}}));
}}
And the whole code can be found here: Click
I would appreciate any help, thanks :)