Creating object arrays without the object speeding up

Hi, this is my first post :blush:

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 :folded_hands:

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;
  }
}

Welcome @Pat ,

Experiment a lot!
Save what works and copy it to a different version to experiment.
Add comments.
I number my from 000, 001, … 999.

The draw() loop updates 60 frames per second:

Some options:

circles[i] = new Circle(1, random(200,600)); // speed is 1 but could be smaller

Slow things down with frameRate():

More advance is a timer to control when you update.
Do a search in the forum for this.

There are examples of particle systems here:

:)

You mean, something like this sketch:
https://OpenProcessing.org/sketch/1805772

I think you need to move (not copy) this line
float circleX = 880; into the class