Newbie - Trying to make a "Taiko"-like game

That requires a small timer and then the boules.add(... part from above. The position of the ball is randomly, see command random().

A timer looks like this:


if(millis() - timeStarted > 1000){

      boules.add(new Boule.........
      timeStarted=millis();

}

Example

Example not using your code above…


ArrayList<Ball> balls = new ArrayList();

int timeStarted=0; 

void setup() {
  size(800, 800);
  noStroke();
  timeStarted=-1000;
} // func 

void draw() {
  background(255);

  // for-loop
  for (int i = balls.size()-1; i >= 0; i--) { 
    Ball ball = balls.get(i);
    ball.move();
    ball.display();
  } // for 

  // a new for-loop to delete balls
  // (it is better to have 2 separate for-loops)
  for (int i = balls.size()-1; i >= 0; i--) { // going backwards 
    Ball ball = balls.get(i);
    if (ball.y+ball.h > height) {
      balls.remove(i);
    } // if
  } // for

  checkTimer();

  fill(0); //black
  text(balls.size(), 
    20, height-20);
} // func 

void checkTimer() {
  // timer

  int rectWidth = 20;
  int rectHeight = 20;

  float rCol, gCol, bCol;

  if (millis() - timeStarted > 1000) {
    // new ball
    rCol = random(255);
    gCol = random(255);
    bCol = random(255);

    balls.add(new Ball(random(width), random(-60, 60), 
      rectWidth, rectHeight, 
      rCol, gCol, bCol));

    timeStarted=millis();
  }
}

// ==================================================

class Ball {  
  // pos
  float x;
  float y;

  // size
  float w;
  float h;

  // movement 
  float speed = 5;

  // color // or use color 
  float r, g, b;

  // constr
  Ball(float tempX, float tempY, 
    float tempW, float tempH, 
    float tempR, float tempG, float tempB) {

    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;

    r = tempR;
    g = tempG;
    b = tempB;
  }// constr

  void move() {
    y = y + speed;
  }

  void display() {
    fill(r, g, b);
    ellipse(x, y, w, h);
  }
  //
} // class
//
1 Like