Your issue isn't the most obvious thing in the world, but it comes from your for-loop and how you're updating your circles' x positions:
for (let i = circles.length - 1; i >= 0; i--) {
circles[i].update(i * 200)
When you delete a circle, the subsequent circles all shift down one index in your array, meaning if you delete the circle at index 0
, the circle at index 1 now becomes the circle at index 0, which when you apply your cricles[i].update(i * 200)
in the next call to draw()
, i*200
will evaluate to 0
, which causes your circle to shift into the "kill zone".
Instead, you store an internal circleNum
property in your circles when you create them, and adjust your position based on that:
class createCircle {
constructor(circleNum) {
this.circle = [];
this.pos = 0;
this.xPos = width / 5;
this.circleNum = circleNum;
this.create();
}
...
}
Then when you create your circle, pass in the index:
//Create new Circle
for (let i = 0; i < circleNumber; i++) {
circles.push(new createCircle(i))
}
also, change how you call .update()
:
circles[i].update(200);
and lastly, use circleNum
when calculating the new pos (as circleNum will remain consistent unlike i
):
this.pos = this.xPos + (this.circleNum*dist) / 1.8;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…