# Loops and object movement

**URL:** <https://discourse.processing.org/t/loops-and-object-movement/20384>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 2, 2020, 10:09am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384 "2020-05-02T10:09:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![s6277](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@s6277](https://discourse.processing.org/u/s6277)\
**Post date:** [May 2, 2020, 10:09am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/1 "2020-05-02T10:09:54Z")

</div>

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

---

<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:** [May 2, 2020, 10:12am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/2 "2020-05-02T10:12:55Z")

</div>

That’s really only possible when you show your code

---

<div class="post-metadata">

**Author:** ![s6277](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@s6277](https://discourse.processing.org/u/s6277)\
**Post date:** [May 2, 2020, 10:17am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/3 "2020-05-02T10:17:19Z")

</div>

Im not really allowed to show my code

---

<div class="post-metadata">

**Author:** ![s6277](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@s6277](https://discourse.processing.org/u/s6277)\
**Post date:** [May 2, 2020, 10:18am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/4 "2020-05-02T10:18:51Z")

</div>

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

---

<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:** [May 2, 2020, 10:50am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/5 "2020-05-02T10:50:09Z")

</div>

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

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

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

---

<div class="post-metadata">

**Author:** ![Java.SourceForger](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/java.sourceforger/32/9831_2.png) [@Java.SourceForger](https://discourse.processing.org/u/Java.SourceForger)\
**Post date:** [May 2, 2020, 4:27pm UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/6 "2020-05-02T16:27:49Z")

</div>

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.

---

<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:** [May 28, 2020, 10:12am UTC](https://discourse.processing.org/t/loops-and-object-movement/20384/7 "2020-05-28T10:12:18Z")

</div>

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/](https://www.processing.org/tutorials/2darray/)

I hope this helps!

Chrisir
