Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Smoother JS Sprite Movement

OhNoItsA8

New Coder
I'm in the process of making a simple, 2D racing game. I have some code that works to move the car with the arrows, but it isn't very smooth. Once it gets to the end of the page, it scrolls as the car moves, and it's very shaky to the point where it hurts to watch. If you want to see what I mean, here's the link to the game's current webpage: https://cargame.aidankierstead.repl.co/game.html The code I currently have is:
var objImage = null; function init() { objImage = document.getElementById("image1"); objImage.style.position = "relative"; objImage.style.left = "0px"; objImage.style.top = "0px"; } function getKeyAndMove(e) { var key_code = e.which || e.keyCode; switch (key_code) { case 37: //left arrow key moveLeft(); break; case 38: //Up arrow key moveUp(); break; case 39: //right arrow key moveRight(); break; case 40: //down arrow key moveDown(); break; } } function moveLeft() { objImage.style.left = parseInt(objImage.style.left) - 5 + "px"; } function moveUp() { objImage.style.top = parseInt(objImage.style.top) - 5 + "px"; } function moveRight() { objImage.style.left = parseInt(objImage.style.left) + 10 + "px"; } function moveDown() { objImage.style.top = parseInt(objImage.style.top) + 5 + "px"; } window.onload = init;

Thanks for any help!
 

Buy us a coffee!

Back
Top Bottom