Skip to content
Advertisement

Basic keyboard functionality for movement of object

I am unable to figure out what is wrong with my code. I have another example I was able to get to work but it didn’t use constructor objects and this one does. That’s about the only different I can think of. I’ve tweaked it in many ways but no luck. Please help me understand why it isn’t working.

JavaScript

Advertisement

Answer

Your loop only runs once. requestAnimationFrame(loop); is like setTimeout you need to call it for each frame. Add the line requestAnimationFrame(loop); at the bottom of the function loop.

Example

JavaScript

Re comments

The code is a mess and I am unsure as to your intentions in parts of it.

I have re-written it as follows making guesses as to your intentions.

JavaScript
JavaScript

Take what you can from it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement