# Using Loops to Make Objects. Help!

**URL:** <https://discourse.processing.org/t/using-loops-to-make-objects-help/1621>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 9, 2018, 1:07pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621 "2018-07-09T13:07:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 1:07pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/1 "2018-07-09T13:07:09Z")

</div>

So i’m back here again. I’m trying to figure out: is it possible to use let’s say a for loop to draw an object 3 times and be able to control them individually still?

What I’m trying to do is make the object appear 3 times and have each object bounce off the sides of the screen one after another. I don’t want them all to move and bounce at the same time. From my understanding it’s not possible but I figured I’d come ask the pros for some help.

Thanks!

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 1:12pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/2 "2018-07-09T13:12:03Z")

</div>

Hi  
I suppose your objects are created from a class?  
You can create them like this:

```auto
ArrayList<ClassName> objectlist = new ArrayList<ClassName>();
objectlist.add(new ClassName());

```

And then control each one using a for-loop:

```auto
for (int i = 0; i < objectlist.size(); i++) {
  objectlist.get(i).move();
}

```

or

```auto
for (ClassName obj : objectlist) {
  obj.move();
}

```

I hope this helps.  
Max

---

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 1:16pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/3 "2018-07-09T13:16:41Z")

</div>

I’m actually trying to do this for a school project and we only just started learning loops so all of that is way over my head.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 1:17pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/4 "2018-07-09T13:17:19Z")

</div>

Post your code so I can be more specific.

---

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 1:22pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/5 "2018-07-09T13:22:53Z")

</div>

void setup()  
{  
size (400,400);  
smooth();  
}  
int diam = 20;  
int x1 = 10;  
int y1 = 10;  
int x2 = 50;  
int y2 = 50;  
int x3 = 90;  
int y3 = 90;  
int xDir = 1;  
int yDir = 1;  
int xDir2 = 1;  
int yDir2 = 1;  
int xDir3 = 1;  
int yDir3 = 1;  
int r, g, b;

void draw()  
{  
background(r, g, b);

fill(255 - (x1 + y1) \* .6375 / 2);  
ellipse(x1, y1, diam, diam);

fill(255 - (x2 + y2) \* .6375 / 2);  
ellipse(x2, y2, diam, diam);

fill(255 - (x3 + y3) \* .6375 / 2);  
ellipse(x3, y3, diam, diam);

if(x1 \> width - diam / 2 || x1 \< diam / 2)  
{  
xDir = -xDir;  
}  
if(x2 \> width - diam / 2 || x2 \< diam / 2)  
{  
xDir2 = -xDir2;  
}  
if(x3 \> width - diam / 2 || x3 \< diam / 2)  
{  
xDir3 = -xDir3;  
}

if(y1 \> height - diam / 2 || y1 \< diam / 2)  
{  
yDir = -yDir;  
}  
if(y2 \> height - diam / 2 || y2 \< diam / 2)  
{  
yDir2 = -yDir2;  
}  
if(y3 \> height - diam / 2 || y3 \< diam / 2)  
{  
yDir3 = -yDir3;  
}

x1 = x1 + xDir;  
y1 = y1 + yDir;  
x2 = x2 + xDir2;  
y2 = y2 + yDir2;  
x3 = x3 + xDir3;  
y3 = y3 + yDir3;  
}

void keyPressed()  
{  
if(key == ENTER || key == RETURN)  
{  
r = (int) random(255);  
g = (int) random(255);  
b = (int) random(255);  
}  
}

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 1:30pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/6 "2018-07-09T13:30:17Z")

</div>

Ok.  
When you look at your code you notice that you basically did the same thing 3 times, the only difference is the starting position, right?!

Now I want you to stare at this until you think you understand it and then post your new code, using this as a start, where there are 3 balls flying again. Then we’ll continue 🙂

```auto
int diam = 20;
int r, g, b;
myObject ball1;

void setup()
{
  size (400, 400);
  smooth();
  
  ball1 = new myObject(10, 10);
}

void draw()
{
  background(r, g, b);
  
  ball1.show();
  ball1.move();
}

void keyPressed()
{
  if (key == ENTER || key == RETURN)
  {
    r = (int) random(255);
    g = (int) random(255);
    b = (int) random(255);
  }
}

class myObject {
  int x = 0;
  int y = 0;
  int xDir = 1;
  int yDir = 1;

  myObject(int x1, int y1) {
    x = x1;
    y = y1;
  }

  void show() {
    fill(255 - (x + y) * .6375 / 2);
    ellipse(x, y, diam, diam);
  }

  void move() {
    if (x > width - diam / 2 || x < diam / 2)
    {
      xDir = -xDir;
    }

    if (y > height - diam / 2 || y < diam / 2)
    {
      yDir = -yDir;
    }

    x = x + xDir;
    y = y + yDir;
  }
}

```

Here’s a tip:  
You don’t need to create a “myObject2”.

---

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 1:34pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/7 "2018-07-09T13:34:41Z")

</div>

i got nothing. Like I said I haven’t learned any of this stuff yet. I’m not up to classes and the like just yet so I was trying to see if I could keep it simple. And my professor said we had to use a loop to draw multiple objects but then says we have to have them bounce one after another so I wasn’t sure how that was possible.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 1:41pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/8 "2018-07-09T13:41:30Z")

</div>

It is possible using classes.

A class is like having a blueprint of how to build a human. Every human is different (=position of the ball) but they all (you know what i mean) can walk (=move and bounce).

So this class called myObject is the blueprint of your ball. It contains a function to move the ball:

> [@MxFxM](#):
>
> x = x + xDir; y = y + yDir;

and to have it bounce:

> [@MxFxM](#):
>
> if (x \> width - diam / 2 || x \< diam / 2) {  
> xDir = -xDir;  
> }  
> if (y \> height - diam / 2 || y \< diam / 2) {  
> yDir = -yDir;  
> }

At the beginning we create a **new** ball accoring to that blueprint and for doing that we give it it’s starting position 10, 10 in the case of the first ball.

In the draw function we tell that ball we created to move and then tell it to show itself (draw the ellipse).

You got that so far?

---

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 1:43pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/9 "2018-07-09T13:43:30Z")

</div>

yes so far i can understand that.

---

<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:** [July 9, 2018, 1:46pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/10 "2018-07-09T13:46:28Z")

</div>

1. [http://Forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays](http://Forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays)
2. [http://Forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes](http://Forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes)

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 1:46pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/11 "2018-07-09T13:46:46Z")

</div>

Fine.  
So now you want another ball to appear.  
The second ball is also a ball so we can use the same blueprint.  
What you will have to do is make a place to save the second ball (object):

> [@MxFxM](#):
>
> myObject ball2;

create it like that (for nr 2):

> [@MxFxM](#):
>
> ball2 = new myObject(50, 50);

(there is the other starting position btw.)  
and have it do the same stuff as the first one:

> [@MxFxM](#):
>
> ball2.show();  
> ball2.move();

Now do that for the 3rd ball too.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 2:06pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/12 "2018-07-09T14:06:31Z")

</div>

Well once you’re done you might say: “dude now it’s the same but way more complex”. And that’s kind of true. But firstly it’s nicer to look at and secondly, now you can do this:

```auto
int r, g, b;
int num_of_balls = 100;
Ball[] balls = new Ball[num_of_balls];

void setup()
{
  size (400, 400);
  smooth();
  
  for (int i = 0; i < balls.length; i++) {
    balls[i] = new Ball((int)random(50, width - 50), (int)random(50, height - 50));
  }
}

void draw()
{
  background(r, g, b);
  
  for (int i = 0; i < balls.length; i++) {
    balls[i].show();
    balls[i].move();
  }
}

void keyPressed()
{
  if (key == ENTER || key == RETURN)
  {
    r = (int) random(255);
    g = (int) random(255);
    b = (int) random(255);
  }
}

class Ball {
  int x = 0;
  int y = 0;
  int xDir = 1;
  int yDir = 1;
  int diam = 20;

  Ball(int x1, int y1) {
    x = x1;
    y = y1;
  }

  void show() {
    fill(255 - (x + y) * .6375 / 2);
    ellipse(x, y, diam, diam);
  }

  void move() {
    if (x > width - diam / 2 || x < diam / 2)
    {
      xDir = -xDir;
    }

    if (y > height - diam / 2 || y < diam / 2)
    {
      yDir = -yDir;
    }

    x = x + xDir;
    y = y + yDir;
  }
}

```

PS: I cleaned up a bit more…

---

<div class="post-metadata">

**Author:** ![QuazArxx](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@QuazArxx](https://discourse.processing.org/u/QuazArxx)\
**Post date:** [July 9, 2018, 2:36pm UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/13 "2018-07-09T14:36:45Z")

</div>

This is great to learn so thank you for that, but I still won’t be able to use it in my school project so I’ll have to figure out what else I can do or just risk losing points.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 11, 2018, 6:45am UTC](https://discourse.processing.org/t/using-loops-to-make-objects-help/1621/14 "2018-07-11T06:45:05Z")

</div>

If you really don’t want to use classes (which is the way to go in an object oriented language) you might use this aswell:

```auto
void setup()
{
  size (400, 400);
  smooth();
}
int diam = 20;
int[] x = {10, 50, 90};
int[] y = {10, 50, 90};
int[] xDir = {1, 1, 1};
int[] yDir = {1, 1, 1};
int r, g, b;

void draw()
{
  background(r, g, b);

  for (int i = 0; i < 3; i++) {
    fill(255 - (x[i] + y[i]) * .6375 / 2);
    ellipse(x[i], y[i], diam, diam);

    if (x[i] > width - diam / 2 || x[i] < diam / 2)
    {
      xDir[i] = -xDir[i];
    }

    if (y[i] > height - diam / 2 || y[i] < diam / 2)
    {
      yDir[i] = -yDir[i];
    }

    x[i] = x[i] + xDir[i];
    y[i] = y[i] + yDir[i];
  }
}

void keyPressed()
{
  if (key == ENTER || key == RETURN)
  {
    r = (int) random(255);
    g = (int) random(255);
    b = (int) random(255);
  }
}

```

But you won’t be able to do it without arrays.
