Spacebar Press not working when game is published and running in VSCode LiveServer extension.
Hello,
I am developing a little game in AnimateCC HTML5 Canvas scripts, trying to create a "flappy bird" effect when the user presses the space bar. It works fine when I test it on the browser, however, after publishing it as HTML and trying to run it on a Live Server in VSCode (it doesn't run on the browser after publishing) the spacebar press is not working at all. But the rest of the game seems to be just fine.
Here is a snippet of my code:
var spacebar_pressed = false;
var gameStart = false;
var userVelocity = 0;
var gravity = 0.5;
var user = this.user;
window.onkeydown = function (event) {
if (event.keyCode == 32) {
gameStart = true;
spacebar_pressed = true;
};
};
window.onkeyup = function () {
if (event.keyCode == 32) {
spacebar_pressed = false;
};
}
function fallDown() {
user.y = user.y + userVelocity;
userVelocity = userVelocity + gravity;
}
function flyUp() {
userVelocity = -15;
user.play();
}
function animate() {
if (spacebar_pressed) {
flyUp();
} else if (gameStart) {
fallDown();
};
requestAnimationFrame(animate);
};
animate();
Any ideas on why it isn't working?
Thank you.
