Making multiple lanes using arrays

Hi ! I am having issues trying to make multiple lanes for a project. I have made one lane using a loop as it is a dashed lane. I am struggling to make multiple of those lanes in my game using an array. Can someone please help me?

You have one for loop now for the dashed line

The line is at a certain y and leading to the right


Now surrounded the entire for loop (from for to } )
with a 2nd outer for loop that tells you how many lines you want to have underneath each other.

The array could represent the y values for the lines.

So before setup make the array

int[] listY = {
103,
160,
230

};

outer for loop

for(int index = 0; index<3; index ++) {
   for (...
     line(..., listY[index], ...., listY[index]);
   }
}

fill in the blank

I am getting some sort of error.

1 Like

Check your brackets{}

No code allowed outside the functions like draw

Remember to use ctrl-t to get auto indent

You only will get real help when posting your code as text

Presumably after the for loops have 2 instead of 3 }

I did some coding for that according to what you said but my lanes have disappeared.

Here is my coding:

<
void draw() {
//dashed horizontal line
background(255);

for(int index = 0; index<3; index ++) {
for(varx=0; varx<=width;varx = varx + 40);
line(varx, height/4, varx+30, height/4);
}

No semicolon here

It closes the for loop

I removed the semicolon. I still do not see multiple lanes. It is back to the one lane.

Show me the code

Chrisir

You also removed { } around the inner for loop

Why?

because it said unexpected token.

<

void draw() {
//dashed horizontal line
background(255);

for(int index = 0; index<3; index ++) {
for(varx=0; varx<=width;varx = varx + 40)
line(varx, height/4, varx+30, height/4);
}

for(int index = 0; index<3; index ++) {
for(varx=0; varx<=width;varx = varx + 40) {
stroke(0);
line(varx, height/4, varx+30, height/4);
}
}

i still do not see multiple lanes. Maybe the error is in the array making.
<

// Array

int[] listY = {
103,
160,
230

};

This array code is before set up

You don’t use the array

Please see the way I did it

The 2 for loops are in draw, right? After background?

In the section where showGameOverScreen is false amd you set the variable to false before setup ()?

Of course since you always use this instead of the array the 3 lines are on top of each other

Same y values for all

Instead use array as shown

Like here please

Thank you!

Chrisir

I still don’t understand what you mean. When i change the value of heigth/a no., it changes the shape of the dash and makes it diagonal. I’m so sorry it is taking me a while to understand.

Replace height/4 in both places with the array

listY[index]

oh okay and how do we change the number of lanes we want? For example when we substitue 10 in the following for 4, it should change to 4 lanes:

<

final int N_CARS_IN_LANE = 10;

In this line please

Chrisir