[Solved] sprite.setVisible(false) freezes the update loop

I’m making an object catching game

I have a character sprite standing in the corner with idle animation playing (blinking, moving slightly, etc).

And objects falling from above - apples.

The player has to catch these apples by clicking on them.

When the player clicks on apples these events happen:

1.The apple object moves with movetoObject towards a target with its xy hidden on that character sprite. So basically the apple kinda falls in the character’s basket.
2. The sprite’s idle animation is hidden and changes to the jumping animation (he jumps up to catch the apple into his basket).

Afterwards the animation changes back to idle.
This is done with a zone overlap, with a small zone hidden at the movetoObject’s target’s coordinates.
In the update function: when the zone gets overlapped the jumping animation gets hidden and the idle animation gets visible again. I also want to hide the game object (apple) after it gets “caught” by the character. However this doesn’t work for some reason. When I add apple.setVisible to false, and the apple reaches the target, the game freezes completely. If i don’t add the setVisible set to false the apple just lies there in the corner in front of the character sprite, but other stuff works fine.

Here is the problematic part of the code:

function update ()
{

info.setText('Score: ’ + score );

var touching = zone.body.touching;

if (!touching.none) {
this.catchPlayer.setVisible(false);
this.idlePlayer.setVisible(true);
apple.setVisible(false);

}

}

Why does the setVisible for apple physics object freeze the game loop while the character sprite’s setVisible works fine? What am I doing wrong? Thank you very much!

Ok, I found the solution. The debugger said apple was undefined. Looks like the update function could not see the variable from the create function. I had to make this variable global. I need to study JavaScript more. :blush: