Having an object leave a streak with an ArrayList

Hello,

in this sketch i tried to have an object leave a streak by using an arraylist. So what i want to achieve is to draw multiple rectangles that lose opacity and size in every frame.

I'm new to coding and i can't seem to get my ArrayList to work properly. It only draws one rectangle even though it's supposed to draw 3 on three different locations.

ArrayList<Class1> streak = new ArrayList<Class1>();
int posX =0, posY=200;
int posX1, posY1;
int Yteil =150;
int amount = 3;

void setup() {
  size (1000, 500);
  for (int i = 0; i < amount; i++) {
    streak.add(new Class1(posX, Yteil));
    Yteil = Yteil-20;
  }
}

void draw () {
  strokeWeight(0);
  background(50);
  if (posX < 800) {
    posX++;
  }
  rect(posX, 250, 30, 30);

  println(posX, posX1, Yteil);

  streak.get(0).drawClass1();
}

class Class1 {
  float value;
  int size=30;

  Class1(int x, int y) {
    posX = x;
    posY = y;
  }
  void drawClass1() {
    // if (size > 0) size--;
    // fill(c) alpha

    rect(posX, Yteil, size, size);
  }
}

Answers

  • edited July 2017

    Hello!

    Your problem is you have not fully implemented your class yet.

    The idea of a class is that it holds all data necessary for the objects derived from the class.

    • Can you tell which data you forgot to move inside the class?

    Also remember that although we have let's say 4 or 100 elements (objects) in the ArrayList we only need each property for each streak once inside the class. This is because the objects (cookies) derived from the class (cookie maker) are isolated from each other and each object copies everything it needs to know about itself from the class.

    There is a great tutorial that you might want to read (and try) again:

    https://www.processing.org/tutorials/objects/

    Further Remarks:

    • Moreover, this line streak.get(0).drawClass1(); displays one rectangle (item 0) from the ArrayList, what about the other two?

    • Also, Class1 is not such a good name. Why not go with ClassRectangle or even just Rectangle?

    • More later.

    Best, Chrisir ;-)

Sign In or Register to comment.