1

I'm very new to Processing and coding in general and trying to program a row of falling Domino bricks activated by an ellipse. I have programmed a function for the bricks standing upright and one for the fallen bricks, but I can only get the bricks to fall all at the same time. I'm looking for a way to make them fall one after the other. It would be great if someone could help me out.

This is my Code so far - First tab:

Dom[] dominos = new Dom[20];
int m;
float x = 100;

void setup() {
  size (600, 600);
  for (int i=0; i < dominos.length; i++) {
    dominos[i] = new Dom();
  }
}

void draw() {
  background(0);

  if (m<91) {
    m = m + 1;
  }

  fill(0);
  ellipse(m, height/2 + 15, 20, 20);

  fill(255, 80, 0);
  ellipse (m, height/2 + 15, 20, 20);

  for (int i=0; i < dominos.length; i++) {
    if (m < 90)
      dominos[1].show();

    if (m >= 90)
      dominos[i].fall();
  }
}

Second tab:

class Dom {
  float x = 100;
  float y = height/2 - 22.5;

  void fall() {
    push();
    stroke(255);
    strokeWeight(10);
    strokeCap(SQUARE);

    for (int i = 0; i<15; i++) {
      line (x + i*30 + 45, y+40, x + i *30, y+50);
    }
    pop();
  }

  void show() {
    push();
    stroke(255);
    strokeWeight(10);
    strokeCap(SQUARE);

    for (int i = 0; i<15; i++) {
      line (x + i*30, y, x + i *30, y+45);
    }
    pop();
  }
}``

1 Answers1

1

First of all, look what Your Dom.fall(), and Dom.show() is doing.
Each method draws 15 rectangles.
Next, look what Are You doing with dominos. It's an Array of 20 elements.
So, in draw() You are drawing 15 rectangles, 20 times with every frame refresh.

Basically You need an Array of 15 objects each drawing one rectangle. Or one object drawing 15 rectangles. But not 20 objects drawing 15 rectangles.

So, here is the simpler solution of Your problem:

Dom dominos;
int m;

void setup() {
  size (600, 600);
  dominos = new Dom();
}

void draw() {
  background(0);
  if (m<91) {
    m++;
  }

  fill(255, 80, 0);
  ellipse(m, height/2 + 15, 20, 20);

  if (m>=90) {
      dominos.fallenNumber++;
  }
  dominos.draw();
}

class Dom {
  float x = 100;
  float y = height/2 - 22.5;
  int fallenNumber = 0;

    void draw() {
        push();
        stroke(255);
        strokeWeight(10);
        strokeCap(SQUARE);
        for (int i=0; i<15; i++) {
            if (i<fallenNumber){
                    line (x + i*30 + 45, y+40, x + i *30, y+50);
                } else {
                    line (x + i*30, y, x + i *30, y+45);
            }
        }  
        pop();
    }
}
Mruk
  • 171
  • 1
  • 11