So im trying to learn about canvas and canvas games and im currently (more or less) following the W3Schools tutorial on canvas games.
At some point in the tutorial i had the idea to make 2 players, which should both be controled on the same keyboard (NOT online multiplayer).
So i followed the logic given by the tutorial and found the key codes for both WASD and the arrows.
I understand 95% of my code, which means i did not just copy everything without understanding it. (I will comeback to this soon)
The problem with my code is that when i add another player to the system i can move them freely when i only control one player at a time, when i try to move both players at the same time, they cannot be moved freely and i can only press a total of 4 buttons at a time.
Tryout the snippet and play around with the cubes with WASD and the arrows to see what im talking about.
As i said there is a part i do not understand 100% which might be the place for this error? I marked it out anyway on the code snippet.
So all in all my question is: Why can i not move both players freely at the same time?
The whole code is as following and i marked the part i do not understand:
For best experience, use the full screen function
{
function startGame() {
myGameArea.start();
myStick = new component(100, 100, 200, 200, "red");
myStick2 = new component(100, 100, 600, 200, "green");
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
var bodyID = document.getElementById("body");
this.canvas.width = bodyID.offsetWidth;
this.canvas.height = (bodyID.offsetHeight);
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[2]);
this.interval = setInterval(updateGameArea, (1000 / 60));
//The part i do not understand
window.addEventListener('keydown', function (e) {
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = true;
});
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = false;
});
//End
},
clear : function(){
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
};
function component(width, height, x, y, color, mLeft, mRight, mUpLeft, mUpRigth){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.speedX = 0;
this.speedY = 0;
this.update = function(){
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
this.newPos = function(){
this.x += this.speedX;
this.y += this.speedY;
};
this.player1 = function(){
this.speedX = 0;
this.speedY = 0;
if (myGameArea.keys && myGameArea.keys[65]) {this.speedX = -2; } // Left
if (myGameArea.keys && myGameArea.keys[68]) {this.speedX = 2; } // Right
if (myGameArea.keys && myGameArea.keys[87]) {this.speedY = -2; } // Up
if (myGameArea.keys && myGameArea.keys[83]) {this.speedY = 2; } // Down
};
this.player2 = function(){
this.speedX = 0;
this.speedY = 0;
if (myGameArea.keys && myGameArea.keys[37]) {this.speedX = -2; } // Left
if (myGameArea.keys && myGameArea.keys[39]) {this.speedX = 2; } // Right
if (myGameArea.keys && myGameArea.keys[38]) {this.speedY = -2; } // Up
if (myGameArea.keys && myGameArea.keys[40]) {this.speedY = 2; } // Down
};
}
function updateGameArea(){
myGameArea.clear();
myStick.player1();
myStick.newPos();
myStick2.player2();
myStick2.newPos();
myStick.update();
myStick2.update();
}
}
.nm{
margin: 0;
padding: 0;
}
canvas{
display: block;
background-color: lightgray;
}
<html>
<head>
<meta charset="UTF-8">
<title>Canvas stick game!</title>
<link rel="stylesheet" href="css/standard.css">
</head>
<body id="body" onload="startGame()" class="nm" style="height: 100vh">
</body>
</html>
<script src="js/canvas.js"></script>