Create randomly placed circles loop not working(runtime error)

So I am making a game where you would click on randomly appearing circles, and for the sake of reusability and self-improvement I have chosen to use for loops and arrays for the first time.

After some unexpected results I have gotten this piece of code, and I have no idea what is causing it to completely not work.

The game would look like
aimbooster.com


float fRandomX,fRandomY;
int randomX, randomY;
void game() {
  currentColor=colors[1];
    for (int i=0; i==4; i++) {
     fRandomX=random(20, 580); 
     fRandomY=random(20, 580);
     randomX=round(fRandomX);
     randomY=round(fRandomY);

    targetX[i]=randomX;
    targetY[i]=randomY;
  }
    for (int i = 0; i ==4; i++) {
    ellipse(targetX[i], targetY[i], size/2.0, size/2.0);
  }

}

If that Is not enough code,

int size=40, difficulty;
float targetDelay;
boolean hasStarted, hasLost, menuState, gameState;

int currentColor;  // color system

int[] colors={#264653,#2A9D8F,#E9C46A,#F4A261};

int[] targetX, targetY;

int  lastScore; 



void setup(){
 ellipseMode(CENTER);
 rectMode(CORNERS);
 size(600,600); 
 frameRate(60);
}
void draw(){
  background(currentColor);
check();
debug();

  
}
void check(){
   if(!hasStarted ||hasLost){
 menuState=true;        //run the menu 
 hasStarted=true;    //dont run it again
 hasLost=false;      //reset game if lost 
 }
 if(menuState){
   menu(lastScore);
 }else if(gameState){
  game(); 
 }
 
}

void menu(int score1){
  currentColor=colors[0];
  ellipse(100,100,30,30);//gameButton
  
  text("last Score:"+score1,50,50);
}
void mousePressed(){
 if(dist(mouseX,mouseY,100,100)<30){
   gameState=true;
   menuState=false;
 }
}


void debug(){
 text(mouseX+";"+mouseY,20,20); 
}
1 Like

not much better…

int size=40, difficulty;
float targetDelay;
boolean hasStarted=false, hasLost=false, menuState=true, gameState=false;

int currentColor;  // color system

int[] colors={#264653, #2A9D8F, #E9C46A, #F4A261};

int[] targetX=new int[4], targetY=new int[4];

int  lastScore; 

float fRandomX, fRandomY;
int randomX, randomY;

//-------------------------------------------------------------------------

void setup() {
  size(600, 600); 
  ellipseMode(CENTER);
  rectMode(CORNERS);
  frameRate(1);
}

void draw() {
  background(currentColor);
  check();
  debug();
}

//-------------------------------------------------------------------------

void check() {
  if (!hasStarted ||hasLost) {
    menuState=true;        //run the menu 
    hasStarted=true;    //dont run it again
    hasLost=false;      //reset game if lost
  }
  if (menuState) {
    menu(lastScore);
  } else if (gameState) {
    game();
  }
}

void game() {

  currentColor=colors[1];

  for (int i=0; i<4; i++) {
    fRandomX=random(20, 580); 
    fRandomY=random(20, 580);
    randomX=round(fRandomX);
    randomY=round(fRandomY);

    targetX[i]=randomX;
    targetY[i]=randomY;
  }

  for (int i = 0; i<4; i++) {
    fill(colors[i]); 
    ellipse(targetX[i], targetY[i], 
      size/2.0, size/2.0);
  }
}

void menu(int score1) {
  currentColor=colors[0];
  ellipse(100, 100, 30, 30);//gameButton

  text("last Score:"+score1, 50, 50);
}

void mousePressed() {
  if (dist(mouseX, mouseY, 100, 100)<30) {
    gameState=true;
    menuState=false;
  }
}

void debug() {
  text(mouseX+";"+mouseY, 20, 20);
}
//

The nullpointer error was happening because of the array not having a max amount of elements.

int[] targetX= new int[10], targetY=new int[10];

Now the problem lies in the fact that the circles are moving too fast. I have no idea on how to slow down a for loop or maybe even not change the coordinates if the circle has not been clicked yet. I will make them clickable after I find out how to make them not disappear instantly.

1 Like

here is a timer


int size=40, difficulty;
float targetDelay;
boolean hasStarted=false, hasLost=false, menuState=false, gameState=true;

int currentColor;  // color system

int[] colors={#264653, #2A9D8F, #E9C46A, #F4A261};

int[] targetX=new int[4], 
  targetY=new int[4];

int lastScore; 

float fRandomX, fRandomY;
int randomX, randomY;

int timer; 
boolean firstTime=true; 

//-------------------------------------------------------------------------

void setup() {
  size(600, 600); 
  ellipseMode(CENTER);
  rectMode(CORNERS);
  frameRate(99);

  timer=millis();
}

void draw() {
  background(currentColor);
  check();
  debug();
}

//-------------------------------------------------------------------------

void check() {
  if (!hasStarted ||hasLost) {
    menuState=true;        //run the menu 
    hasStarted=true;    //dont run it again
    hasLost=false;      //reset game if lost
  }
  if (menuState) {
    menu(lastScore);
  } else if (gameState) {
    game();
  }
}

void game() {

  currentColor=colors[1];

  // timer
  if (millis()-timer > 3500 || firstTime) {
    // define 
    for (int i=0; i<4; i++) {
      fRandomX=random(20, 580); 
      fRandomY=random(20, 580);
      randomX=round(fRandomX);
      randomY=round(fRandomY);

      targetX[i]=randomX;
      targetY[i]=randomY;
    }
    timer=millis();
    firstTime=false;
  }

  // show
  for (int i = 0; i<4; i++) {
    fill(colors[i]); 
    ellipse(targetX[i], targetY[i], 
      size/2.0, size/2.0);
  }
}

void menu(int score1) {
  currentColor=colors[0];
  ellipse(100, 100, 30, 30);//gameButton

  text("last Score:"+score1, 50, 50);
}

void mousePressed() {
  if (dist(mouseX, mouseY, 100, 100)<30) {
    gameState=true;
    menuState=false;
  }
}

void debug() {
  text(mouseX+";"+mouseY, 20, 20);
}
//