Treasure game: how to pick up items and end the game?

When you code hit = true; your next line needs to be show[id] = false; We need to have a counter or id so we know what mine or treasure number you just passed over. Just beneath void opSchatGeklikt() you need to initialize a variable for the id, eg int id = 0; Then at the bottom of the inside most loop you need to code for id++; (just beneath if (hit){}). This will increment the id for every item in the inner loop. I assume this would be the same as the index of the treasure object, but this may or may not be the case. Somehow you have to have the array index of the treasure object to match the index (id) of the loop where you trap the hit. Once this is done check the show array before you draw the object.

Second edit:
The id++; needs to go below the if(hit){…} function. We don’t want to increment the id only when an object is hit we need to have an id for every object.

Third edit:
With regards to the null point exception: Did you initialize the show array and fill it with true values?

Fourth edit:
put a println(id); right under that id++; Let’s see what the id range is. It should be between 0 and 5 (assuming you only have five treasures.)

Fifth edit:
What does rect(schattenCoor[i][0], schattenCoor[i][1], breedte,breedte); draw?

Sixth edit:
initialize show array like this: boolean show = new boolean[5]; Assuming you only have five treasures.

Seventh edit:
Why are you checking 200 objects? Looks like you would only need to check five?

Edit 8:
This looks complicated. What is it doing: if (dist(xDuiker, yDuiker, schattenCoor[i][0], schattenCoor[i][1]) <= breedte && grid[j][k] == 3). We may have to come up with a simpler way to know if you’re over a treasure and then only check five objects.

Edit 9:
We need that id to go to five. Move id++ up one line of code, right under if(hit){…}

Edit 10:
Now put that println() right under hit = true; Let’s see if it knows which treasure you moved over.

now it goes to my game over screen when I’m on a treasure.

Edit 11:
That may not be good; is that what you intended?

Edit 12:
Try putting id++ up inside the loop, right below rect();

Edit 13:
The index (i) that you use to draw the black rectangle should be the same one that you use to set show[I] = false;

Edit 14:
To make sure I understand the goal, you want the treasure to turn black when you move over it and then not be drawn again. Is this correct?

Edit 15:
We may not need the id; now that you have simplified it, we may be able to use the “i” index that you already have.

Edit 16:
Use println(i); to see which one you’re making black.

Edit 17:
See what the value for “i” is. Then we can set show[i] = false. Instead of painting the treasure black we’ll tell it to mark it as not to be drawn again.

Edit 18:
Hallelujah!! If you know i = 0 to 4 then you have the index for your treasure. UnREM the line show[i] = false;

if i do this do i also need to unREM the boolean show = new boolean[5]?

Edit 19:
Yes, unREM it; you can’t use it you don’t initialize it.

Edit 20:
Put right after the first boolean; the correct syntax is boolean[] show = new boolean[5];

Edit 21:
Where are you drawing the treasure to start with? We need to put a loop in there to check the show array before your app draws every treasure.

Edit 22:
See Edit 21.

Edit 23:
Put a loop in that function to looks something like this:

for (int i = 0; i < show.length(); i++) {
  if (show[i] == true) {
    drawMyTreasure code
  }
}

Edit 24:
Ok, just hard code it with a five, since we already know how many treasures there are.