# Create randomly placed circles loop not working(runtime error)

**URL:** https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719
**Category:** Coding Questions
**Created:** [May 30, 2019, 8:12pm UTC](https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719 "2019-05-30T20:12:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dakito](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@dakito](https://discourse.processing.org/u/dakito)
#### Post date: [May 30, 2019, 8:12pm UTC](https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719/1 "2019-05-30T20:12:18Z")

</div>

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](http://aimbooster.com)

```auto

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,

```auto
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); 
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 30, 2019, 8:41pm UTC](https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719/2 "2019-05-30T20:41:44Z")

</div>

not much better…

```auto
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);
}
//

```

---

<div class="post-metadata">

### Author: ![dakito](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@dakito](https://discourse.processing.org/u/dakito)
#### Post date: [May 31, 2019, 7:26pm UTC](https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719/3 "2019-05-31T19:26:47Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 31, 2019, 10:39pm UTC](https://discourse.processing.org/t/create-randomly-placed-circles-loop-not-working-runtime-error/11719/4 "2019-05-31T22:39:34Z")

</div>

here is a timer

```auto

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);
}
//

```
