I am unable to figure out what is wrong with my code. I have another example I was able to get to work but it didn’t use constructor objects and this one does. That’s about the only different I can think of. I’ve tweaked it in many ways but no luck. Please help me understand why it isn’t working.
JavaScript
x
53
53
1
function newGame() {
2
let Player, Controller;
3
let context = document.getElementById("canvas").getContext("2d");
4
//Player
5
Player = function (x, y, width, height) {
6
this.width = width,
7
this.height = height,
8
this.x = x,
9
this.y = y,
10
this.xVelocity = 0;
11
this.yVelocity = 0;
12
this.update = function () {
13
context.fillStyle = "red";
14
context.fillRect(this.x + this.xVelocity, this.y + this.yVelocity, this.width, this.height);
15
};
16
};
17
let player1 = new Player(200, 200, 25, 25);
18
19
let playerUpdate = function () {
20
player1.update();
21
};
22
23
//Controller
24
Controller = function() {
25
this.right = false;
26
this.left = false;
27
this.keyDownUp = function(e) {
28
let keyInput = (e.type == "keydown") ? true : false;
29
console.log(keyInput)
30
switch (e.keyCode) {
31
case 37:
32
this.left = keyInput;
33
break;
34
case 39:
35
this.right = keyInput;
36
}
37
}
38
};
39
40
let loop = function () {
41
if (Controller.left) {
42
player1.xVelocity += 10;
43
};
44
playerUpdate();
45
};
46
47
window.requestAnimationFrame(loop);
48
window.addEventListener("keydown", Controller.keyDownUp);
49
window.addEventListener("keyup", Controller.keyDownUp);
50
}
51
52
newGame();
53
Advertisement
Answer
Your loop
only runs once. requestAnimationFrame(loop);
is like setTimeout
you need to call it for each frame. Add the line requestAnimationFrame(loop);
at the bottom of the function loop
.
Example
JavaScript
1
10
10
1
function loop() {
2
if (Controller.left) {
3
player1.xVelocity += 10;
4
}
5
playerUpdate();
6
requestAnimationFrame(loop); // get next frame
7
};
8
requestAnimationFrame(loop); // start animation
9
10
Re comments
The code is a mess and I am unsure as to your intentions in parts of it.
I have re-written it as follows making guesses as to your intentions.
JavaScript
1
44
44
1
(() => {
2
function Player(x, y, width, height) {
3
this.width = width,
4
this.height = height,
5
this.x = x,
6
this.y = y,
7
this.vx = 0;
8
this.vy = 0;
9
}
10
Player.prototype = {
11
update() {
12
this.vx = controller.left ? -10 : 0;
13
this.vx = controller.right ? 10 : this.vx;
14
this.x += this.vx;
15
this.y += this.vy;
16
this.x = (this.x + ctx.canvas.width) % ctx.canvas.width;
17
},
18
draw() {
19
ctx.fillStyle = "red";
20
ctx.fillRect(this.x, this.y, this.width, this.height);
21
}
22
}
23
function Controller() {
24
this.right = false;
25
this.left = false;
26
addEventListener("keydown", keyEvent.bind(this));
27
addEventListener("keyup", keyEvent.bind(this));
28
function keyEvent(e) {
29
if (e.code === "ArrowRight") { this.right = e.type === "keydown" }
30
else if (e.code === "ArrowLeft") { this.left = e.type === "keydown" }
31
}
32
}
33
function loop() {
34
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
35
player.update();
36
player.draw();
37
requestAnimationFrame(loop);
38
};
39
40
const ctx = document.getElementById("canvas").getContext("2d");
41
const controller = new Controller();
42
const player = new Player(200, 175, 25, 25);
43
requestAnimationFrame(loop);
44
})();
JavaScript
1
1
1
<canvas id="canvas" width="300" height="200"></canvas>
Take what you can from it.