# Mouse clicked with arrays issue (wolves and lambs)

**URL:** <https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [February 2, 2023, 8:15pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807 "2023-02-02T20:15:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 8:15pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/1 "2023-02-02T20:15:26Z")

</div>

Hi! So there’s this assignment i need to do. I made a game where you basically have to find a wolf inside a pack of sheep. Once you find it, you have to click on it, and it will move on to the next level. You need to find as many wolves as you can before the time runs out. However, my teacher told me to make more than one wolf appear by using arrays, and I can’t seem to make it work!

The array itself works, since it shows the number of wolves you decide, but the mouse clicked function doesn’t. I don’t understand arrays 100%, so there’s probably something I’m missing, or perhaps there’s another way of doing it that i don’t know about. I just can’t find what it is I’m doing wrong.

Here’s the code. Sorry if it’s messy or something, I’m doing my best 😢

```auto
PImage lobo;
PImage oveja;
PImage fondo;
PImage caja;
PImage gameover;

int GameOver=1; //cuándo termina la partida
int Restart=1;
int Puntos=0;

//variables lobo
int nlobo=5;

int loboX[]=new int[nlobo];
int loboY[]=new int[nlobo];

//variables ovejas
//aumenta el valor de n para subir la dificultad
int n=3;
int ovejasX[]=new int[n];
int ovejasY[]=new int[n];

int ms=10; //milisegundos

//cambiar tiempo aquí
int s=90; //segundos

void setup(){
  size(800,800);

  //gráficos
  lobo = loadImage("lobo.png");
  oveja = loadImage("oveja.png");
  fondo = loadImage("fondo.png");  
  caja = loadImage("fondo2.png");
  gameover = loadImage("gameover.png");

  
  for(int contador=0;contador<n;contador++){
    ovejasX[contador]=int(random(0,677));
    ovejasY[contador]=int(random(100,654)); //el random es así para que no se salgan de la pantalla
}

for(int contadorL=0;contadorL<nlobo;contadorL++){
   loboX[contadorL]=int(random(0,677));
   loboY[contadorL]=int(random(100,677));
}

}
    

  
void draw(){
  
  //tiempo //lo pongo aquí porque se solapaban los números
  
  image(caja, 0, 0);
  textAlign(CENTER);
  textSize(30);
  text(s,630,50);

   if(ms<=100 && ms>0){
    ms=ms-1;
   }
   
   if(ms==0){

     s=s-1;
     ms=100;
     
     if(s<=0){
    
      noLoop();
      fill(255,0,0);
      GameOver=0;
    
      //desaparecen las ovejas y se revela la posición del lobo
      background(255,255,255);
    
      textAlign(CENTER);
      textSize(100);
      image(gameover, 0, 0);
      textSize(30);
      text("¡Se acabó el tiempo!",400,500);
      text("Lobos cazados: " + (Puntos),400,550);
      }
   }  

  
  
  //juego
  
  if((GameOver==1) && (Restart==1)){
    DibujarFormas();
  }
}
  
  
void DibujarFormas(){
  
  image(fondo, 0, 0);
  
  //caja de texto
  fill(0,0,0);
  rect(0,0,800,70);
  
 //objeto principal (lobo). Lo pongo delante para que los las ovejas lo pisen. Porque se está escondiendo
  
  for(int contadorL=0;contadorL<nlobo;contadorL++){
   image(lobo, loboX[contadorL], loboY[contadorL]);
  
 //ovejas
 fill(255,255,255);
  for(int contador=0;contador<n;contador++){
   image(oveja, ovejasX[contador], ovejasY[contador]);

  }
  
 //tiempo
  fill(255,255,255);
  textAlign(CENTER);
  textSize(30);
  text("Tiempo: ",560,50);
  
  
 //puntos
  fill(255,255,255);
  textAlign(CENTER);
  textSize(30);
  text("Puntos: " + (Puntos),230,50);
   
  
 //nueva pantalla
  Restart=0;
    
}
}

//para que salga la mano sobre el lobo
void mouseMoved(){
  
  for(int contadorL=0;contadorL<nlobo;contadorL++){
   loboX[contadorL]=int(random(0,677));
   loboY[contadorL]=int(random(100,677));
   
  if((mouseX > loboX[contadorL]) && (mouseX < loboX[contadorL]+123) && (mouseY > loboY[contadorL]) && (mouseY < loboY[contadorL]+160))
   {
    cursor(HAND);
   }

   else{
    cursor(ARROW); 
   }
}
}

void mouseClicked(){
  
 if(GameOver==1) //si la partida está iniciada
 {
  if(Restart==0) //y no está reiniciado
    {
   Restart=1; //se reinicia cuando le das click
     }
     
  for(int contadorL=0;contadorL<nlobo;contadorL++){  
   loboX[contadorL]=int(random(0,677));
   loboY[contadorL]=int(random(100,677));
   
  if((mouseX > loboX[contadorL]) && (mouseX < loboX[contadorL]+123) && (mouseY > loboY[contadorL]) && (mouseY < loboY[contadorL]+160)) //si haces click en el lobo ganas puntos
   {
     
    Puntos++;  

   }
   
   
   else{ //si haces click en cualquier otro sitio, y la partida está iniciada, game over
   
   noLoop();
     fill(255,0,0);
     GameOver=0;
    
    //desaparecen las ovejas y se revela la posición del lobo
    background(255,255,255);
    
    for(int contadorLI=0;contadorLI<nlobo;contadorLI++){
      
    image(lobo, loboX[contadorLI], loboY[contadorLI]);
    }
    
    textAlign(CENTER);
    textSize(30);
    image(gameover, 0, 0);
    text("¡El lobo se ha escapado!",400,500);
    text("Lobos cazados: " + (Puntos),400,550);

   }   
 }  
}
}

```

---

<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:** [February 2, 2023, 8:43pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/2 "2023-02-02T20:43:31Z")

</div>

**remark**

> [@andprocessing](#):
>
> ```auto
> for(int contadorL=0;contadorL<nlobo;contadorL++){
> image(lobo, loboX[contadorL], loboY[contadorL]);
> 
> ```

The } should be here, instead it’s much further down

---

<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:** [February 2, 2023, 8:52pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/3 "2023-02-02T20:52:44Z")

</div>

> [@andprocessing](#):
>
> ```auto
> loboX[contadorL]=int(random(0,677));
> loboY[contadorL]=int(random(100,677));
> 
> ```

why do you do this?

Do you want the wolves to move during game?

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 8:57pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/4 "2023-02-02T20:57:30Z")

</div>

no, I don’t. I just want them to appear in a random position. Should i remove those 2 lines?

---

<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:** [February 2, 2023, 8:59pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/5 "2023-02-02T20:59:26Z")

</div>

you need to do the random once only

in setup and when starting a new game

not throughout please

---

<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:** [February 2, 2023, 9:01pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/6 "2023-02-02T21:01:06Z")

</div>

> [@andprocessing](#):
>
> ```auto
> loboX[contadorL]=int(random(0,677));
> loboY[contadorL]=int(random(100,677));
> 
> ```

you have this in mouseMoved and in mouseClicked

delete both

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 9:04pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/7 "2023-02-02T21:04:23Z")

</div>

done! \<3

```auto
void mouseMoved(){
  
  for(int contadorL=0;contadorL<nlobo;contadorL++){
   
  if((mouseX > loboX[contadorL]) && (mouseX < loboX[contadorL]+123) && (mouseY > loboY[contadorL]) && (mouseY < loboY[contadorL]+160))
   {
    cursor(HAND);
   }

   else{
    cursor(ARROW); 
   }
}
}

void mouseClicked(){
  
 if(GameOver==1) //si la partida está iniciada
 {
  if(Restart==0) //y no está reiniciado
    {
   Restart=1; //se reinicia cuando le das click
     }
     
  for(int contadorL=0;contadorL<nlobo;contadorL++){  
   
  if((mouseX > loboX[contadorL]) && (mouseX < loboX[contadorL]+123) && (mouseY > loboY[contadorL]) && (mouseY < loboY[contadorL]+160)) //si haces click en el lobo ganas puntos
   {
     
    Puntos++;  

   }
   
   
   else{ //si haces click en cualquier otro sitio, y la partida está iniciada, game over
   
   noLoop();
     fill(255,0,0);
     GameOver=0;
    
    //desaparecen las ovejas y se revela la posición del lobo
    background(255,255,255);
    
    for(int contadorLI=0;contadorLI<nlobo;contadorLI++){
      
    image(lobo, loboX[contadorLI], loboY[contadorLI]);
    }
    
    textAlign(CENTER);
    textSize(30);
    image(gameover, 0, 0);
    text("¡El lobo se ha escapado!",400,500);
    text("Lobos cazados: " + (Puntos),400,550);

   }   
 }  
}
}

```

still doesn’t work, though

---

<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:** [February 2, 2023, 9:17pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/8 "2023-02-02T21:17:28Z")

</div>

Yeah.

Look at the code where you set the mouse cursor to hand or arrow.

```auto
  for (int contadorL=0; contadorL<nlobo; contadorL++) {

    if ((mouseX > loboX[contadorL]) &&
      (mouseX < loboX[contadorL]+123) &&
      (mouseY > loboY[contadorL]) &&
      (mouseY < loboY[contadorL]+160))
    {
      cursor(HAND);
    } else {
      cursor(ARROW);
    }//else 
  }//for

```

let’s say we hit wolf #2 then the cursor is HAND, but the loop continues, but the mouse is not on wolf #3 and not on wolf #4, so the cursor return to Arrow. Bad.

Your code works when the mouse is over the **last** wolf in the array. 😉

Your for loop should register one wolf and not go back to Arrow.

So we can place `cursor(ARROW);` **before** the for-loop (default is arrow) and in the for-loop only have cursor(HAND); when we hit any wolf.

```auto

  cursor(ARROW); // !!!!!!!!!!!

  for (int contadorL=0; contadorL<nlobo; contadorL++) {

    if ((mouseX > loboX[contadorL]) &&
      (mouseX < loboX[contadorL]+123) &&
      (mouseY > loboY[contadorL]) &&
      (mouseY < loboY[contadorL]+160))
    {
      cursor(HAND);
    } else {
     // cursor(ARROW); // NO !!!!!!!!!!!!!!!!!!!!
    }//else 
  }//for

```

SAME GOES FOR THE FOR-LOOP in mouseClick!!!

Same problem!!!

* * *

for example, when you hit wolf #2, puntos go UP, but wolf #3 is not hit, so it produces “¡El lobo se ha escapado!”, which is an Error.

**IDEA**

you can make a counter for the wolves that have been hit. **After** the entire for-loop, the counter should be **1**. Only when it’s 0, we show the message “¡El lobo se ha escapado!” :

```auto
void mouseClicked() {

  if (GameOver==1) //si la partida está iniciada
  {
    if (Restart==0) //y no está reiniciado
    {
      Restart=1; //se reinicia cuando le das click
    }

    int counter = 0; // !!!!!!!!!!!!!!!!!!!!!!!

    for (int contadorL=0; contadorL<nlobo; contadorL++) {

      if ((mouseX > loboX[contadorL]) &&
        (mouseX < loboX[contadorL]+123) &&
        (mouseY > loboY[contadorL]) &&
        (mouseY < loboY[contadorL]+160))
        //si haces click en el lobo ganas puntos
      {

        Puntos++;
        counter++; // !!!!!!!!!!!!!!!!!!!!!!!
      }//if
    }//for

    if ( counter == 0 ) // !!!!!!!!!!!!!!!!!!!!!!!!!!!
    {
      //si haces click en cualquier otro sitio, y la partida está iniciada, game over

      // noLoop();
      fill(255, 0, 0);
      GameOver=0;

      //desaparecen las ovejas y se revela la posición del lobo
      background(255, 255, 255);

      for (int contadorLI=0; contadorLI<nlobo; contadorLI++) {

        //image(lobo, loboX[contadorLI], loboY[contadorLI]);
      }

      textAlign(CENTER);
      textSize(30);
      // image(gameover, 0, 0);
      text("¡El lobo se ha escapado!", 400, 500);
      text("Lobos cazados: " + (Puntos), 400, 550);
    }
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 9:51pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/9 "2023-02-02T21:51:20Z")

</div>

wow, now I understand, THANK YOU SO MUCH!!! This is great help. Now it works perfectly!

But when you click on them, the sheep and wolves don’t get a new random position (which did work for me when i didn’t program the arrays), any idea on how to make that happen? I’m guessing it’s also because of the array.

---

<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:** [February 2, 2023, 11:10pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/10 "2023-02-02T23:10:41Z")

</div>

> [@andprocessing](#):
>
> the sheep and wolves don’t get a new random position

My bad.

Then you have to insert your random lines again!!!

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 11:31pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/11 "2023-02-02T23:31:10Z")

</div>

when I do that it goes back to not working ☹

here’s what i wrote in case i missed something

```auto
void mouseMoved(){
  
cursor(ARROW); // !!!!!!!!!!!

  for (int contadorL=0; contadorL<nlobo; contadorL++) {
    
    loboX[contadorL]=int(random(0,677));
     loboY[contadorL]=int(random(100,677));

    if ((mouseX > loboX[contadorL]) &&
      (mouseX < loboX[contadorL]+123) &&
      (mouseY > loboY[contadorL]) &&
      (mouseY < loboY[contadorL]+160))
    {
      cursor(HAND);
    } else {
     // cursor(ARROW); // NO !!!!!!!!!!!!!!!!!!!!
    }//else 
  }//for
}

  
 void mouseClicked() {
   

  if (GameOver==1) //si la partida está iniciada
  {
    if (Restart==0) //y no está reiniciado
    {
      Restart=1; //se reinicia cuando le das click
    }

    int counter = 0; // !!!!!!!!!!!!!!!!!!!!!!!

    for (int contadorL=0; contadorL<nlobo; contadorL++) {
      
       loboX[contadorL]=int(random(0,677));
       loboY[contadorL]=int(random(100,677));
      
      
      if ((mouseX > loboX[contadorL]) &&
        (mouseX < loboX[contadorL]+123) &&
        (mouseY > loboY[contadorL]) &&
        (mouseY < loboY[contadorL]+160))
        //si haces click en el lobo ganas puntos
      {

        Puntos++;
        counter++; // !!!!!!!!!!!!!!!!!!!!!!!
      }//if
    }//for

    if ( counter == 0 ) // !!!!!!!!!!!!!!!!!!!!!!!!!!!
    {
      //si haces click en cualquier otro sitio, y la partida está iniciada, game over

      // noLoop();
      fill(255, 0, 0);
      GameOver=0;

      //desaparecen las ovejas y se revela la posición del lobo
      background(255, 255, 255);

      for (int contadorLI=0; contadorLI<nlobo; contadorLI++) {
         loboX[contadorLI]=int(random(0,677));
         loboY[contadorLI]=int(random(100,677));

        image(lobo, loboX[contadorLI], loboY[contadorLI]);
      }

      textAlign(CENTER);
      textSize(30);
      // image(gameover, 0, 0);
      text("¡El lobo se ha escapado!", 400, 500);
      text("Lobos cazados: " + (Puntos), 400, 550);
    }
  }

}
//

```

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 11:33pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/12 "2023-02-02T23:33:57Z")

</div>

however, when i get the game over screen, the wolves do move places, so it seems like it does fix that, but it goes back to not being able to click it properly ☹

---

<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:** [February 2, 2023, 11:50pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/13 "2023-02-02T23:50:28Z")

</div>

> [@andprocessing](#):
>
> But when you click on them, the sheep and wolves don’t get a new random position

you need to place the lines

```auto
     loboX[contadorL]=int(random(0,677));
     loboY[contadorL]=int(random(100,677));

```

in the right position.

For example: do you want every sheep and every wolf to move?

Or only the wolf you clicked on (where puntos goes up?)

do you want every sheep and every wolf to move only when it’s game over??

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 2, 2023, 11:56pm UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/14 "2023-02-02T23:56:14Z")

</div>

I want every sheep and every wolf to move.

While waiting for your response I started trying different things and I got the sheep and the wolf you click on to move, but the other wolves stay in the same position, so right now that’s the only thing I can’t get to work. I guess I’m positioning that line wrong?

```auto
void mouseClicked() {
   

  if (GameOver==1) //si la partida está iniciada
  {
    if (Restart==0) //y no está reiniciado
    {
      Restart=1; //se reinicia cuando le das click
    }

    int counter = 0; // !!!!!!!!!!!!!!!!!!!!!!!

    for (int contadorL=0; contadorL<nlobo; contadorL++) {
      
      
      if ((mouseX > loboX[contadorL]) &&
        (mouseX < loboX[contadorL]+123) &&
        (mouseY > loboY[contadorL]) &&
        (mouseY < loboY[contadorL]+160))
        //si haces click en el lobo ganas puntos
      {

         loboX[contadorL]=int(random(0,677));
         loboY[contadorL]=int(random(100,677)); //wolves, only one changes position

       
         for(int contador=0;contador<n;contador++){
           
           ovejasX[contador]=int(random(0,677));
           ovejasY[contador]=int(random(100,654)); //sheep
         }
           
        
        Puntos++;
        counter++; // !!!!!!!!!!!!!!!!!!!!!!!
        
      }//if
    }//for

    if ( counter == 0 ) // !!!!!!!!!!!!!!!!!!!!!!!!!!!
    {
      //si haces click en cualquier otro sitio, y la partida está iniciada, game over

      // noLoop();
      fill(255, 0, 0);
      GameOver=0;

      //desaparecen las ovejas y se revela la posición del lobo
      background(255, 255, 255);

      for (int contadorLI=0; contadorLI<nlobo; contadorLI++) {

        image(lobo, loboX[contadorLI], loboY[contadorLI]);
      }

      textAlign(CENTER);
      textSize(30);
      // image(gameover, 0, 0);
      text("¡El lobo se ha escapado!", 400, 500);
      text("Lobos cazados: " + (Puntos), 400, 550);
    }
  }

}

```

---

<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:** [February 3, 2023, 12:00am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/15 "2023-02-03T00:00:27Z")

</div>

To  
Move all wolves you need a for loop like you have for the sheep

---

<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:** [February 3, 2023, 12:02am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/16 "2023-02-03T00:02:03Z")

</div>

> [@andprocessing](#):
>
> ```auto
> if ( counter == 0 ) // !!!!!!!!!!!!!!!!!!!!!!!!!!!
> {
> 
> ```

Maybe you want to love the wolves in this section?

When no wolf has been clicked?

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 3, 2023, 12:08am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/17 "2023-02-03T00:08:20Z")

</div>

it’s giving me an error when I do that… ArrayOutOfBoundsException

---

<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:** [February 3, 2023, 12:11am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/18 "2023-02-03T00:11:22Z")

</div>

> [@andprocessing](#):
>
> ```auto
> for(int contador=0;contador<n;contador++){
>           
> 
> ```

Don’t use this for loop, use the for loop with L variables!!!

---

<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:** [February 3, 2023, 12:11am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/19 "2023-02-03T00:11:52Z")

</div>

How can I tell what you did when you don’t show me your code???

---

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 3, 2023, 12:16am UTC](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807/20 "2023-02-03T00:16:24Z")

</div>

I’m sorry, here’s what I wrote

```auto
void mouseClicked() {
   

  if (GameOver==1) //si la partida está iniciada
  {
    if (Restart==0) //y no está reiniciado
    {
      Restart=1; //se reinicia cuando le das click
    }

    int counter = 0; // !!!!!!!!!!!!!!!!!!!!!!!

    for (int contadorL=0; contadorL<nlobo; contadorL++) {
      
      
      if ((mouseX > loboX[contadorL]) &&
        (mouseX < loboX[contadorL]+123) &&
        (mouseY > loboY[contadorL]) &&
        (mouseY < loboY[contadorL]+160))
        //si haces click en el lobo ganas puntos
      {
        
         loboX[contadorL]=int(random(0,677));
         loboY[contadorL]=int(random(100,677));
        
        

       
         for(int contador=0;contador<n;contador++){
           
           ovejasX[contador]=int(random(0,677));
           ovejasY[contador]=int(random(100,654)); //sheep
         }
           
        
        Puntos++;
        counter++; // !!!!!!!!!!!!!!!!!!!!!!!
        
      }//if
    }//for

    if ( counter == 0 ) // !!!!!!!!!!!!!!!!!!!!!!!!!!!
    {
      //si haces click en cualquier otro sitio, y la partida está iniciada, game over
      
       for(int contadorL=0;contadorL<n;contadorL++){
         loboX[contadorL]=int(random(0,677));
         loboY[contadorL]=int(random(100,677)); //wolves
         }

      // noLoop();
      fill(255, 0, 0);
      GameOver=0;

      //desaparecen las ovejas y se revela la posición del lobo
      background(255, 255, 255);

      for (int contadorLI=0; contadorLI<nlobo; contadorLI++) {

        image(lobo, loboX[contadorLI], loboY[contadorLI]);
      }

      textAlign(CENTER);
      textSize(30);
      // image(gameover, 0, 0);
      text("¡El lobo se ha escapado!", 400, 500);
      text("Lobos cazados: " + (Puntos), 400, 550);
      

    }
  }

}

```

[Next page](https://discourse.processing.org/t/mouse-clicked-with-arrays-issue-wolves-and-lambs/40807.md?page=2)
