# Mouse clicked with arrays issue (wolves and lambs)

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

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

ovejasY[contador]=int(random(100,654)); //el random es así para que no se salgan de la pantalla
}

}

}

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

//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

//ovejas
fill(255,255,255);

}

//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(){

{
cursor(HAND);
}

else{
cursor(ARROW);
}
}
}

void mouseClicked(){

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

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

}

textAlign(CENTER);
textSize(30);
image(gameover, 0, 0);

}
}
}
}

``````

remark

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

1 Like

why do you do this?

Do you want the wolves to move during game?

1 Like

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

1 Like

you need to do the random once only

in setup and when starting a new game

2 Likes

you have this in mouseMoved and in mouseClicked

delete both

1 Like

done! <3

``````void mouseMoved(){

{
cursor(HAND);
}

else{
cursor(ARROW);
}
}
}

void mouseClicked(){

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

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

}

textAlign(CENTER);
textSize(30);
image(gameover, 0, 0);

}
}
}
}
``````

still doesn’t work, though

Yeah.

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

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

{
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.

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

{
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!” :

``````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; // !!!!!!!!!!!!!!!!!!!!!!!

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

}

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

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.

1 Like

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

1 Like

when I do that it goes back to not working

here’s what i wrote in case i missed something

``````void mouseMoved(){

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

{
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; // !!!!!!!!!!!!!!!!!!!!!!!

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

}

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

}
//

``````

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

you need to place the lines

``````     loboX[contadorL]=int(random(0,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??

1 Like

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?

``````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; // !!!!!!!!!!!!!!!!!!!!!!!

//si haces click en el lobo ganas puntos
{

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

}

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

}

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

}
``````

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

Maybe you want to love the wolves in this section?

When no wolf has been clicked?

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

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

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

I’m sorry, here’s what I wrote

``````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; // !!!!!!!!!!!!!!!!!!!!!!!

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

}

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

}
}

}
``````