Skip to main content
Participant
January 2, 2023
Question

Spacebar Press not working when game is published and running in VSCode LiveServer extension.

  • January 2, 2023
  • 1 reply
  • 378 views

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.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 2, 2023

any errors in the developer console?

Participant
January 2, 2023

Not during testing, everything works ok and there are no errors. I don't really know how to open a developer console when running the published work in a server and not a browser...

kglad
Community Expert
Community Expert
January 2, 2023

you're not opening in a browser from that server?