Need help with these codes! (array)

please format code with </> button * homework policy * asking questions

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

Hello!!

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

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

1 Like