Okay so the floatBalloon(posY + 2) part is actually calling the function immediately, not passing a reference to requestAnimationFrame. that’s why it’s recursing instantly and blowing up the call stack, not waiting for the next frame. you need to wrap it in an anonymous function so requestAnimationFrame gets a function to call later. like this:
Spot the Bug answer: The requestAnimationFrame function is called with the result of floatBalloon(posY + 2) instead of a function reference.
The fix:
Change requestAnimationFrame(floatBalloon(posY + 2)); to requestAnimationFrame(() => floatBalloon(posY + 2));
Why:
When floatBalloon(posY + 2) is executed immediately, it returns undefined. requestAnimationFrame then tries to call undefined, which does nothing. The immediate execution of floatBalloon(posY + 2) within the current frame causes the animation to speed up because it’s not waiting for the next frame.
The immediate execution is indeed the critical part here. It’s like trying to play a piano piece by just saying the notes instead of actually pressing the keys.