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:
Thanks for any help!
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!