Loops and object movement

Hey, i’m very confused as to why my rectangle is not moving, I’ve tried everything and i still don’t get it. So i have created four rectangles going in a linear line one under each other, using the for loop. I want to be able to move these four rectangles all together up and down again and again, almost like a bouncing ball, I’ve tried to make it go up and down like a a single ball would but its not working. Can someone please help me.

Thank You

1 Like

That’s really only possible when you show your code

Im not really allowed to show my code

It almost just like a rectangle moving up and down the screen, but instead of one there is now 4

You can use 8 variables to store x,y of each rect:

x1,y1
x2,y2
x3,y3
x4,y4

They can also have different speeds like yspeed1, yspeed2, yspeed3…

And different start positions

In the for loop you can add or substract 1 or yspeed1… from y1…

Next step

You can use arrays:

Instead of

x1,y1
x2,y2
x3,y3
x4,y4

you can use 2 arrays : xArray and yArray - see Tutorial Arrays on the website

Or use one array of PVector (see reference site)

Chrisir

2 Likes

Instead of iterating over a for() loop, try iterating over the draw() Method, outside of the for loop.
The sketch window will only render at the end of the draw method.

2 Likes

when you want to fill a grid with rectangles column by column,
you could store the entire grid as a 2D array (grid).

Then there is a active column with moving rects.

indexActiveColumn holds the index of the 2dArray for x-direction

when you click space bar:

  • indexActiveColumn++;

  • copy rects into the grid or stop them

  • move new rects in the new, now active column

See https://www.processing.org/tutorials/2darray/

I hope this helps!

Chrisir

1 Like