# Creating object arrays without the object speeding up

**URL:** <https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573>\
**Category:** Beginners\
**Created:** [June 18, 2025, 4:47pm UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573 "2025-06-18T16:47:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://discourse.processing.org/u/Pat)\
**Post date:** [June 18, 2025, 4:47pm UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/1 "2025-06-18T16:47:49Z")

</div>

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 🙏

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 18, 2025, 5:20pm UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/2 "2025-06-18T17:20:49Z")

</div>

> [@Pat](#):
>
> Hi, this is my first post

Welcome @Pat ,

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

There are examples here:

> **[Examples](https://processing.org/examples/)**
>
> Short, prototypical programs exploring the basics of programming with Processing.

Check out:

> **[Array Objects / Examples](https://processing.org/examples/arrayobjects.html)**
>
> Demonstrates the syntax for creating an array of custom objects.

and the Particle Systems.

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 18, 2025, 5:46pm UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/3 "2025-06-18T17:46:36Z")

</div>

> [@Pat](#):
>
> , I wanted 10 circles at random Y positions to move from right to left at random speeds…

You mean, something like this sketch:  
[https://OpenProcessing.org/sketch/1805772](https://OpenProcessing.org/sketch/1805772)

> [@Help with simple game (moving fish)](https://discourse.processing.org/t/help-with-simple-game-moving-fish/40679/9):
>
> You pick another value for nextFrameChange only after frameCount reached current nextFrameChange: if (frameCount \>= nextFrameChange) swapDirection(); It’s inside funtion swapDirection() that you change nextFrameChange: void swapDirection() { speed \*= -1; nextFrameChange = frameCount + 60 \* (int) random(5, 11); // 5s to 10s range } Anyways, I did a demo sketch that has a class Fish w/ methods such as display(), update(), bounce(), touchedBoundary(), etc.: Swimming\_Fish.pde /\*\* \* Swim…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 18, 2025, 10:29pm UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/4 "2025-06-18T22:29:17Z")

</div>

> [@Pat](#):
>
> ```auto
> float circleX = 880;
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://discourse.processing.org/u/Pat)\
**Post date:** [June 19, 2025, 10:10am UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/5 "2025-06-19T10:10:32Z")

</div>

@glv Thanks for the encouragement! Yes, I’ve been getting a bit lazy with adding comments - but clearly doing so saves a lot of time when it comes to debugging later!

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://discourse.processing.org/u/Pat)\
**Post date:** [June 19, 2025, 10:11am UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/6 "2025-06-19T10:11:32Z")

</div>

@GoToLoop Great example, I’m going to try recreating this. Thanks 🙏

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://discourse.processing.org/u/Pat)\
**Post date:** [June 19, 2025, 10:12am UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/7 "2025-06-19T10:12:46Z")

</div>

@Chrisir this solves the problem. I think I understand why… Declaring the starting position as a global variable meant that every time an object changed the position, it would remain changed for the next object, and the next, and so on. (Please correct me if I’m wrong 🙂!)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 23, 2025, 9:33am UTC](https://discourse.processing.org/t/creating-object-arrays-without-the-object-speeding-up/46573/8 "2025-06-23T09:33:02Z")

</div>

Hey, glad it helped!

In your first version, circleX was a global var, that means it was shared by all circles.

That means

- all circle had the same x-position and
- not individual x-positions and
- that the decrease in the position (` - speed`) was **10 times per frame** executed (hence the circles got so fast!) and not only once (when circleX is in the class, it’s an object property, so there are 10 individual values and the speed is subtracted only once from each circleX per frame, not 10 times).

When circleX is inside the class, we achieve

- individual values for each circle
- speed is subtracted only once per frame (so it’s slower).

Warm regards, Chrisir
