# Need help with these codes! (array)

**URL:** <https://discourse.processing.org/t/need-help-with-these-codes-array/24940>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 27, 2020, 8:19am UTC](https://discourse.processing.org/t/need-help-with-these-codes-array/24940 "2020-10-27T08:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BettyBai](https://avatars.discourse-cdn.com/v4/letter/b/aeb1de/32.png) [@BettyBai](https://discourse.processing.org/u/BettyBai)\
**Post date:** [October 27, 2020, 8:19am UTC](https://discourse.processing.org/t/need-help-with-these-codes-array/24940/1 "2020-10-27T08:19:19Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

My teacher is asking us to use Array and for loop functions to draw the same image many times. I tried to create new arrays for different variables, but it seems that only one image is shown after running the codes. Could you please help me with it? Thanks!  
Codes:

PImage backgd;  
PImage ultraman;

float x=new float[5];  
float y=new float[5];  
float dx=new float[5];  
float dy=new float[5];  
float imgW=new float[5];  
float imgH=new float[5];  
float imgScale=new float[5];

void setup() {  
backgd = loadImage(“space.png”);  
ultraman = loadImage(“ultra5.png”);  
size(740, 740);  
for(int i=0; i\<x.length;i=i+1)  
{  
x[i] = random(0, width);  
y[i] = random(0, height);  
dx[i] = random(-10, +10);  
dy[i] = random(-10, +10);  
imgScale[i] = random(0.05, 0.35);  
imgW[i] = ultraman.width \* imgScale[i];  
imgH[i] = ultraman.height \* imgScale[i];}  
}

void draw() {  
for(int i=0;i\<x.length;i=i+1)  
{  
image(backgd, 0, 0);  
image(ultraman, x[i], y[i], imgW[i], imgH[i]);

x[i] = x[i] + dx[i];  
if (x[i] \< 0) {  
x[i] = 0;  
dx[i] = -dx[i];  
} else if (x[i]+imgW[i] \> width) {  
x[i] = width - imgW[i];  
dx[i] = -dx[i];  
}

y[i] = y[i] + dy[i];  
if (y[i] \< 0) {  
y[i] = 0;  
dy[i] = -dy[i];  
} else if (y[i]+imgH[i] \> height) {  
y[i] = height - imgH[i];  
dy[i] = -dy[i];  
}}  
}

---

<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:** [October 27, 2020, 10:53am UTC](https://discourse.processing.org/t/need-help-with-these-codes-array/24940/2 "2020-10-27T10:53:53Z")

</div>

Hello!!

Do the changes of x[] etc. BEFORE you show the image. Otherwise they won’t have any effect

---

<div class="post-metadata">

**Author:** ![BettyBai](https://avatars.discourse-cdn.com/v4/letter/b/aeb1de/32.png) [@BettyBai](https://discourse.processing.org/u/BettyBai)\
**Post date:** [October 28, 2020, 4:59am UTC](https://discourse.processing.org/t/need-help-with-these-codes-array/24940/3 "2020-10-28T04:59:53Z")

</div>

Problem Solved!!! Can’t believe I made such a stupid mistake lol. Thank you so much!!! 😄
