Today I'm wondering how I could detect multiple key actions at once. For example, in this code example, I want to be able to press 2 arrow keys at once so that the ball could move diagonally.
Here's the code:
<!DOCTYPE html>
<html>
<head>
<title>Mousemove</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
var circle = function (x, y, radius, fillCircle) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2, false);
if (fillCircle) {
ctx.fill();
} else {
ctx.stroke();
}
};
// The Ball constructor
var Ball = function () {
this.x = width / 2;
this.y = height / 2;
this.xSpeed = 5;
this.ySpeed = 0;
};
// Update the ball's position based on its speed
Ball.prototype.move = function () {
this.x += this.xSpeed;
this.y += this.ySpeed;
if (this.x < 0) {
this.x = width;
} else if (this.x > width) {
this.x = 0;
} else if (this.y < 0) {
this.y = height;
} else if (this.y > height) {
this.y = 0;
}
};
// Draw the ball at its current position
Ball.prototype.draw = function () {
circle(this.x, this.y, 10, true);
};
// Set the ball's direction based on a string
Ball.prototype.setDirection = function() {
var upPressed = keysDown[directions.up];
var downPressed = keysDown[directions.down];
var rightPressed = keysDown[directions.right];
var leftPressed = keysDown[directions.left];
if (upPressed) {
this.ySpeed = -5;
} else if (downPressed) {
this.ySpeed = 5;
}
if (leftPressed) {
this.xSpeed = -5;
} else if (rightPressed) {
this.xSpeed = 5;
}
};
// Create the ball object
var ball = new Ball();
// An object to convert keycodes into action names
var directions = {
left: 37,
up: 38,
right: 39,
down: 40
};
// The keydown handler that will be called for every keypress
var keysDown = {};
$('body').keydown(function(event) {
keysDown[event.keyCode] = true;
}).keyup(function(event) {
keysDown[event.keyCode] = false;
});
// The animation function, called every 30 ms
setInterval(function () {
ctx.clearRect(0, 0, width, height);
ball.draw();
ball.move();
ctx.strokeRect(0, 0, width, height);
}, 30);
</script>
</body>
</html>
If you have tested the code above, you would see that you can move around a little ball with your arrow keys. I was wondering how you could detect multiple arrow keys being pressed down at the same time. So, if I press the down button and the right button, it would move diagonally to the bottom-right corner. How would I code this? An example would be great! Thanks all!