I am making a simple JS game framework and I am trying to make an image that can move around canvas with WASD. The code below should render a movable image but it doesnt do anything. Please help.
HTML
<html>
<head>
<title>raptorjs example</title>
<script src="raptor.js"></script>
</head>
<body>
<canvas id="canvas" width=200 height=200></canvas>
<div style="display:none">
<img src="player.png" alt="d" id="player">
</div>
<script>
var pl = new Player("player",50,50,20,20);
pl.Update= function(){
var x = pl.x
var y = pl.y
if (Keyboard.check("w")){ y -= 1}
if (Keyboard.check("s")){ y += 1}
if (Keyboard.check("a")){ x -= 1}
if (Keyboard.check("d")){ x += 1}
}
</script>
</body>
</html>
JS (framework code)
var canvas = document.getElementById("canvas");
var cv = canvas.getContext("2d")
var pressedKeys = [];
function addKey(ev){
pressedKeys.splice(0,0,ev);
}
function minKey(ev){
var ix = pressedKeys.indexOf(ev);
pressedKeys.splice(ix,1);
}
document.addEventListener("keydown",addKey(e.code));
document.addEventListener("keyup",minKey(e.code));
var Keyboard = new Object();
Keyboard.check = function(k){
if (pressedKeys.indexOf(k)!= -1){
return true;
} else {
return false;
}
}
function delObj(obj){
for (const key in obj){
delete obj[key]
}
}
function Player(img,lx,ly,wid,hig){
this.x = lx;
this.y = ly;
this.width = wid;
this.height = hig;
this.Update = function(){
//playerid.Update = yourFunction()
}
this.Draw = function(){
cv.clearRect(this.x,this.y,this.width,this.height)
var im = document.getElementById(img)
cv.drawImage(im,this.x,this.y,this.width,this.height)
}
function bothf(){
this.Draw()
this.Update()
}
setInterval(bothf(),20)
}
Any help would be greatly appreciated. My main focus is getting the image to appear, but if there are any fixes to the keyboard check function that would be cool too!