Tell us what’s happening: I don’t under stand how calling the animate() function after the click event got fired when we click on the start button and pressing arrow keys will get the movePlayer() called work. All I understand is that after we clicked to the start button, the animate() function would be called and we would see the rectangle moving down a lil bit compared to the default position, but how it work? I dont understand why we passed the animate function itselft to the requestAnimationfuntion(). And about pressing the arrow keys, we only update the x and y values inside the velocity object without calling the update function, so how could the rectangle got redrawn again? Could anyone please explain to me about that?
Your code so far
const startGame = () => {
canvas.style.display = "block";
startScreen.style.display = "none";
animate();
}
const animate = () => {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
player.update();
if (keys.rightKey.pressed && player.position.x < proportionalSize(400)) {
player.velocity.x = 5;
} else if (keys.leftKey.pressed && player.position.x > proportionalSize(100)) {
player.velocity.x = -5;
} else {
player.velocity.x = 0;
}
}
window.addEventListener("keydown", ({ key }) => {
movePlayer(key, 8, true);
});
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Learn Intermediate OOP by Building a Platformer Game - Step 63