Using Loops to Make Objects. Help!

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!

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

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

And then control each one using a for-loop:

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

or

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

I hope this helps.
Max

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.

Post your code so I can be more specific.

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

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

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”.

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.

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:

and to have it bounce:

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?

yes so far i can understand that.

  1. 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

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

create it like that (for nr 2):

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

Now do that for the 3rd ball too.

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:

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…

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.

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:

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.