Lives counter infinite loop. Please help!

Hi! I’m a complete noob in JS and Phaser.

I have this code in the update function:

//if the player fails to catch the veggie and it falls below the floor image cordinate he loses one life.
if (veggie.y > 550 && veggie.x > 150) {
veggie.setVisible(false);
lives -= 1;
}

But when the veggie falls below the floor coordinate the lives continue going down, not stopping, and I get numbers like -346586 lives, looks like I get an infinite loop here. How can I fix it? JS is killing me LOL.
Or maybe I should use a zone overlap or a collider instead of a coordinate?
Thank you so much!

It is because

An invisible Game Object will skip rendering, but will still process update logic.

so you can try checking if visible flag is set to true
if (veggie.y > 550 && veggie.x > 150 && veggie.visible === true)

1 Like

It works! Thank you so much!!!