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