Hi, this is my first post
I’m currently learning to make arrays of objects. Whenever I try this with objects that move across the screen, the speed of the objects increases as the number of objects in the array increases.
In the example below, I wanted 10 circles at random Y positions to move from right to left at random speeds of between 1 and 10 pixels per frame. Instead, the circles move across the screen very fast, and get even faster if I increase the number of circles.
I assume I’m putting the ‘speed’ variable in the wrong place, but I can’t figure it out.
Thanks in advance for any tips
Circle[] circles = new Circle[10];
float circleX = 880;
void setup() {
size(800,800);
for (int i = 0; i < circles.length; i++) {
circles[i] = new Circle(random(1,10), random(200,600));
}
}
void draw() {
background(255);
for (int i = 0; i < circles.length; i++) {
circles[i].drawCircle();
}
}
class Circle {
float speed;
float circleY;
Circle(float tempSpeed, float tempCircleY) {
speed = tempSpeed;
circleY = tempCircleY;
}
void drawCircle() {
fill(0);
noStroke();
ellipse(circleX,circleY,80,80);
circleX = circleX - speed;
}
}