I have an HTML5 game I'm working on and I'm at the point where I'm trying to make the character move. For the most part, the character(A rectangular square) is moving, but it's not moving from its starting position. Instead, when I hit the move key the character's x position and y position are reset to 0, 0. My question is, why is the character's position being set to 0, 0 before it starts moving?
I've included a link to the dropbox with the project so it's easier to see what's going on, but here's the movement script for reference.
initMap.js Script, lines 81 - 92
case "Player":
gameObjectsLayerCtx.fillStyle = "rgb(0, 0, 0)";
gameObjectsLayerCtx.fillRect(xPos, yPos, destWidth / 3, destHeight / 3);
window.onkeydown = function(ev){
var key = ev.key;
if(key == "w"){
yPos += 5;
gameObjectsLayerCtx.clearRect(0, 0, gameObjectsLayer.width, gameObjectsLayer.height);
gameObjectsLayerCtx.fillRect(xPos, yPos, destWidth / 3, destHeight / 3);
}
};
break;
The script is the initMap.js script and the movement functionality is from lines 81 - 92
The character's starting position Where the character moves to when pressing the move key