Thanks for your questions.
Yeah, the boolean variable isDead is part of the class.
It signifies that the cell is dead. Initially isDead is false obviously.
-
isDead plays a role especially when we kill a big cell and replace it with 4 smaller ones (in the function
mousePressed()). It is set to true to mark the old big cell as dead. -
isDead is also used to remove old items from the ArrayList: When isDead it true, we remove (in a backwards for-loop in draw()). The code also works without the removing for-loop (maybe slower since we have more and more dead items in the ArrayList we have to skip).
Why does the code work without the removing for-loop? I’ll explain.
Details
Also for the (very short) time that items in the ArrayList exist that have isDead==true (or when we remove / comment out the removing for-loop), the class ignores dead objects:
- Dead objects are not displayed anymore (in
display()) and - they cannot be clicked. So
onMouse()returns false when cell is dead. So it’s always saying, “No, I have not been pressed”.
Usage of return
These lines
if (isDead)
return; // the oldCell is DEAD and does not display
leave the function draw() before it really begins. But for the rest of the function we know: the cell is alive.
Alternatively you could say if( ! isDead) {.........} and put the entire content of the function in the if clause { … }. (Which would give you a code less clear.)
Usage in the function onMouse()
Similarly:
boolean onMouse() {
if (isDead) // When TRUE return FALSE state! Yes! Cell is not clicked by mouse (or pretends not to be)
return false; // leave with false (mouse is never recognized on a dead cell)
So, when the cell is dead, we leave the function fast - with false.
This means a dead cell can never say, “the mouse has been clicked on me”.
- Which is good because otherwise a dead cell could generate 4 children again and again. Not good.
The further lines of onMouse()
Nothing much about.
Now we read this in onMouse():
// this can return true or false: // I'm not sure what this means? Or why true or false is okay
return
mouseX>x &&
mouseX<x+w &&
mouseY>y &&
mouseY<y+w;
}//method
Well, this just means:
- return true when mouse is inside and
- false when mouse is not inside.
It’s a short form of this long form:
if ( mouseX>x &&
mouseX<x+w &&
mouseY>y &&
mouseY<y+w)
return true; // mouse was inside cell
else return false; // mouse was NOT inside cell
In the short form we just return the result
of this condition directly:
( mouseX>x &&
mouseX<x+w &&
mouseY>y &&
mouseY<y+w)
which is doing the same as the long form.
I hope this helps!
Chrisir