I want to create a tile based RPG game with javascript and the first problem is movement. I'd like to have smooth movement but right now it has somekind of a delay when pressing one key and then the other one right after that. Heres my code of the movement:
function positionSettings() {
document.getElementById("gameWindow").scrollLeft = mapLeft;
document.getElementById("gameWindow").scrollTop = mapTop;
document.getElementById("protagonist").style.left ="507px";
document.getElementById("protagonist").style.top ="347px";
}
function moveMap(keystroke){
switch(keystroke.keyCode){
case 37:
mapLeft = mapLeft - 8;
positionSettings();
break
case 38:
mapTop = mapTop - 8;
positionSettings();
break
case 39:
mapLeft = mapLeft + 8;
positionSettings();
break
case 40:
mapTop = mapTop + 8;
positionSettings();
break
}
}
function loadMap(){
for(updown=0;updown<50;updown++){
for(leftright=0;leftright<50;leftright++){
//alert(tileProperties[leftright][updown]);
var tile = document.createElement("div");
tile.setAttribute("class","mapTile");
if(tileProperties[leftright][updown]){
tile.setAttribute("style","background-color:#00FF00;left:"+ leftright * 32 +"px; top:"+ updown * 32+"px;");
}
else{
tile.setAttribute("style","background-color:#FFFF00;left:"+ leftright * 32 +"px; top:"+ updown * 32+"px;");
}
var tileNum = document.createTextNode(leftright +":"+ updown);
tile.appendChild(tileNum);
document.getElementById("worldMap").appendChild(tile);
}
}
positionSettings();
}
</script>
</head>
<body onload="loadMap()" onkeydown="moveMap(event)">
<div id="gameWindow">
<div id="worldMap">
</div>
</div>
<div id="protagonist">
</div>
</body>