Drawing order of Objects in an Arraylist

That was what I tried to describe before with maxZ and so on.

Let me see…

We first loop over the entire thing and find the clicked tile with the highest Z.

Then after the for-loop we remove it.

(can’t test it here…)



//Remove clicked Object
void mouseClicked() {

  if (ButtonState == 4) {

    int maxZ= -100;  // store the highest Z as maxZ
    int maxZ_Index = -1;  // store the index of the garden object with the highest Z (as maxZ_Index)

    // search highest Z when multiple garden objects are underneath the mouse  
    for (int i=0; i<gardenobjects.size(); i++) {
      if (gardenobjects.get(i).isInside(mouseX, mouseY)) {
        if (gardenobjects.get(i).z > maxZ) {
          maxZ = gardenobjects.get(i).z; // store 
          maxZ_Index = i;
        }//if
      }//if
    }//for

    // -----

    // Evaluate the findings 
    //after the loop delete the garden object with this index.
    if (maxZ_Index > -1) { // found something?
      gardenobjects.remove(maxZ_Index); // Yes
    }//if
    //
  }//if
}//function

(by the way when drawing the garden objects: I wrote 4 there in the for-loop please replace with how many Z-values you really have (this +1, so when you have 0 and 1 as Z-values, you must go to < 2))