# Loop arrayList Crossyroad

**URL:** <https://discourse.processing.org/t/loop-arraylist-crossyroad/258>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [May 22, 2018, 12:22pm UTC](https://discourse.processing.org/t/loop-arraylist-crossyroad/258 "2018-05-22T12:22:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BP3d](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@BP3d](https://discourse.processing.org/u/BP3d)\
**Post date:** [May 22, 2018, 12:22pm UTC](https://discourse.processing.org/t/loop-arraylist-crossyroad/258/1 "2018-05-22T12:22:44Z")

</div>

hi everyone im trying to loop the cars and train but cant figure out how to.

```auto
ArrayList <Car> carsListTop;
ArrayList <Car> carsListBelow;
ArrayList <TreeTrunk> TrunkList;
ArrayList <Train> TrainList;

TreeTrunk [] ArrayTreeTrunk= new TreeTrunk [3]; 
Car [] CarsArr;
Car AutoBovenlevel1, AutoOnderlevel1, Autolevel2;
TreeTrunk Trunk;
Train train;

PImage [] imgcars = new PImage [3];
int [] TrunkYpos= new int [3];

PImage auto1, auto2, auto3;
int imgCarwidth, imgCarHeight;
int randomCar, randomYposCar, CounterJump, counter, randomYposTrunk, randomXposTrunk; 

Chicken chick;

//Background BG1, BG2;
Car Car1, Car2;
PImage bg1, bg2, bg[] = new PImage [2];
int TreeYpos;
boolean level1, level2;
int level; 
int chickXpos, chickYpos;
int count; 
int xPosCar, xPosTrunk, xPosTrain;

void setup()
{
  size(1049, 656);
  level=0;

  bg[0] = loadImage("level1.jpg");
  bg[1] = loadImage("level2.jpg");

  chickXpos=width/2;
  chickYpos=height-50;

  TrunkYpos [0] = 120; 
  TrunkYpos [1] = 150; 
  TrunkYpos [2] = 200;

  xPosCar=0;
  xPosTrunk=0;
  xPosTrain=width;

  imgCarHeight=100;
  imgCarwidth=150;

  randomCar = int ( random( imgcars.length));
  randomYposTrunk = int ( random(TrunkYpos.length));

  chick = new Chicken (chickXpos, chickYpos);

  TrunkList = new ArrayList < TreeTrunk> ();
  carsListTop= new ArrayList <Car>();
  carsListBelow= new ArrayList <Car>();
  TrainList = new ArrayList < Train > ();

  for ( int i =0; i< imgcars.length; i++)
  {
    imgcars[i] = loadImage("C-0"+i+".png");
    imgcars[i].resize(imgCarwidth, imgCarHeight);
  }

  AutoBovenlevel1 = new Car ( xPosCar, height-300, random (0.2, 1), imgcars[randomCar]);
  AutoOnderlevel1 = new Car ( xPosCar, height-180, random (0.2, 1), imgcars[randomCar]);
  Autolevel2 = new Car ( xPosCar, height-150, 2, imgcars[randomCar]);
  Trunk= new TreeTrunk(xPosTrunk, TrunkYpos[randomYposTrunk]);
  train = new Train (xPosTrain, 20);

  //for ( int i =0; i<ArrayTreeTrunk.length; i++)
  //{
  // ArrayTreeTrunk[0]= new TreeTrunk (TrunkXpos[0], TrunkYpos[randomYposTrunk]);
  //}

  ArrayTreeTrunk[0]= new TreeTrunk (width/4, 80);
  ArrayTreeTrunk[1]= new TreeTrunk (width/4+150, 150);
  ArrayTreeTrunk[2]= new TreeTrunk (width/4, 220);

  randomCar= int ( random( imgcars.length));
  randomYposTrunk= int ( random(TrunkYpos.length));
}

void draw()
{

  level();
  Chick();
  //cars();
  //TreeTrunk();
  //TrainShow();
}

void cars ()
{
  if ( level == 1)
  {
    carsListTop.add(AutoBovenlevel1);
    carsListBelow.add(AutoOnderlevel1);
       if (counter== 50 )
    {
      counter=0;
      carsListTop.add( new Car ( xPosCar, height-300, random (0.2, 1), imgcars[randomCar]));
    }
  }

  if (level==2)
  {
    carsListTop.add(Autolevel2);

    println(counter);
    
 
  }
  for ( int i=0; i<carsListTop.size(); i++)
  {
    carsListTop.get(i).move();
    carsListTop.get(i).display();
    //laat cars zien
    println ("start");
  }

  for ( int i=0; i<carsListBelow.size(); i++)
  {
    carsListBelow.get(i).move();
    carsListBelow.get(i).display();
  }

  deleteCars();
}

void deleteCars ()
{
  // delete cars top na dat die het scherm verlaat
  for (int i=0; i<carsListTop.size(); i++)
  {
    if (carsListTop.get(i).checkOutOfScreen() ==true)
    {
      carsListTop.remove(i);
    }
  }

  // deleet cars onders na dat die het scherm verlaat 

  for ( int i =0; i<carsListBelow.size(); i++)
  {
    Car CarBelow = carsListBelow.get(i);
    if (carsListBelow.get(i).checkOutOfScreen() == true)
    {
      println("remove");
      carsListBelow.remove(CarBelow);
      //carsListTop.add(AutoBoven);
      //carsListBelow.add(AutoOnder);
    }
  }
}

void TreeTrunk()
{
  if (level ==1)
  {
    for ( int i=0; i<ArrayTreeTrunk.length; i++)
    {
      ArrayTreeTrunk[i].display();
    }
  }

  if (level ==2)
  {
  }
}

void Chick()
{
  if (level ==1 || level == 2)
  {
    chick.display();
    chick.Bounderie();
    if (keyPressed)
    {
      chick.move();
      chick.jump();
    } else
    {
      chick.keyReleased();
    }
  }
}

void TrainShow()
{
  TrainList.add(train);
  for (int i = 0; i< TrainList.size(); i++)
  {
    TrainList.get(i).move();
    TrainList.get(i).display();

    if (TrainList.get(i).checkOutOfScreenTrain()==true)
    {
      xPosTrain=width; 
      TrainList.add(train);
    }

    if (TrainList.get(i).checkOutOfScreenTrain()==true)
    {
      RemoveTrain();
    }
  }
}
void RemoveTrain()
{
  // Car CarBelow = carsListBelow.get(i);

  for (int i = 0; i< TrainList.size(); i++)
  {
    print("removed train");
    Train trainoutscreen= TrainList.get(i);
    TrainList.remove(trainoutscreen);
  }
}

void level()
{
  if ( level==0)
  {
    fill(255);
    rect(0, 0, width, height);
    level1=false; 
    level2=false;

    fill(0);
    textSize(50);
    text("press space bar", width/2-200, height/2);
    text("to start the game", width/2-220, height/2+50);
    if (keyPressed)
    {
      if ( key == ' ')
      {
        level=1;
      }
    }
  }

  if (level == 1 )
  {    
    level1=true;
    level2=false;
    println(count);
    count ++;
    image(bg[0], 0, 0, width, height);

    Chick();
    TreeTrunk();
    
     fill(0);
    textSize(20);

    text ("jump = spacebar", 10, 20);
    text ("move = arrow keys", 10, 40 );

    //}
    if (chick.GetyPosChick()==10)
    {
      level=2;
      // counter=0;
    }
  }

  if (level==2)
  {
    level2=true;
    level1=false; 
    counter++;
    image(bg[1], 0, 0, width, height);
    Chick();
    TreeTrunk();
    cars();
    TrainShow();
  }
}

void reset ()
{
  if (keyPressed)
  {
    if (key == 'r')
    {
      level =0;
    }
  }
}

void GameOver()
{
  if (chick.getAliveChick()==false )
  {
    textSize(60);
    textMode(CENTER);
    text("GAME OVER", width/2-50, height/2);
    chick.DeadChick();
  }
}

```

i hope someone can help ?

---

<div class="post-metadata">

**Author:** ![deerfeeder](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/deerfeeder/32/149_2.png) [@deerfeeder](https://discourse.processing.org/u/deerfeeder)\
**Post date:** [May 22, 2018, 12:41pm UTC](https://discourse.processing.org/t/loop-arraylist-crossyroad/258/2 "2018-05-22T12:41:30Z")

</div>

Processing app / examples / topics / advanced data / arraylist class

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 22, 2018, 7:31pm UTC](https://discourse.processing.org/t/loop-arraylist-crossyroad/258/3 "2018-05-22T19:31:57Z")

</div>

It’s really hard to help with general “how do I do this” type questions- you’ll have better luck if you post a specific “I tried X, expected Y, but got Z instead” type question.

The best advice I can give you is to [break your problem down into smaller steps](http://happycoding.io/tutorials/how-to/program) and take those steps on one at a time. For example, can you create a small example sketch that uses a hard-coded `ArrayList` and loops through it?

Shameless self-promotion: [here](http://happycoding.io/tutorials/processing/arraylists) is a guide on using `ArrayLists` in Processing. You should also check out [the Processing reference](https://processing.org/reference/ArrayList.html) and the examples that @deerfeeder mentioned above.

If you’re still having trouble, you can post a more specific question along with a [smaller examples](https://stackoverflow.com/help/mcve) that shows where you’re stuck. Good luck!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [May 22, 2018, 8:50pm UTC](https://discourse.processing.org/t/loop-arraylist-crossyroad/258/4 "2018-05-22T20:50:15Z")

</div>

Sounds like you are having trouble making cars go off one end of the screen and then appear at the other end of the screen. A neat trick to deal with this is to just draw each car twice, and correct the position of any car that has gone past the edge.

Example:

```auto
class Foo {
  float px, py, vx;
  color c;
  Foo(float ipy) {
    px = random(width);
    vx = random(0.5,10);
    py = ipy;
    c = color(random(255), random(255), random(255));
  }
  void draw() {
    px+=vx;
    px%=width;
    fill(c);
    rect(px, py, 20, 20);
    rect(px-width, py, 20, 20);
  }
}

ArrayList<Foo> foos = new ArrayList();

void setup() {
  size(600, 600);
  for (int i=0; i<30; foos.add(new Foo((i++)*20)));
}

void draw() {
  background(0);
  for (int i=0; i<30; foos.get(i++).draw());
}

```
