Skip to main content
sammyueru
Participating Frequently
Oct 1, 2021 10:16 PM UTC
Answered

Can't move with scripts - HTML5

So I've been trying to create a game with Adobe

Animate and HTML5 canvas just for fun.

Anyways I can't seem to get player movement to work.

I've tried using:

var canvas = document.getElementById("canvas");
canvas.addEventListener("keydown", doKeyDown, true);
function doKeyDown(e) {
	var playerSpeed = 100;
	//W Key
	if (e.keyCode == 87) {
		this.player.y-=playerSpeed;
	}
	//S Key
	if (e.keyCode == 83) {
		this.player.y+=playerSpeed;
	}
	//A Key
	if (e.keyCode == 65) {
		this.player.x-=playerSpeed;
	}
	//D Key
	if (e.keyCode == 68) {
		this.player.x+=playerSpeed;
	}
}

 And it didn't work, so if you can help please do.

Thanks in advance.

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Oct 1, 2021 10:39 PM UTC

Hi.

 

There's no need to create a reference to canvas because one is automatically created by Animate and is stored in a global variable called canvas.

 

To move characters using keys, there are two samples (simple and advanced) in this link:
https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/move_char_using_keys

 

Please let us know if you haver any further questions.

 

Regards,

JC

4 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Oct 1, 2021 10:39 PM UTC

Hi.

 

There's no need to create a reference to canvas because one is automatically created by Animate and is stored in a global variable called canvas.

 

To move characters using keys, there are two samples (simple and advanced) in this link:
https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/move_char_using_keys

 

Please let us know if you haver any further questions.

 

Regards,

JC

Translate
sammyueru
sammyueruAuthor
Participating Frequently
Oct 1, 2021 11:07 PM UTC

that worked thank you so much

Translate
JoãoCésar17023019
Community Expert
Community Expert
Oct 2, 2021 4:00 PM UTC

You're welcome!

Translate
sammyueru
sammyueruAuthor
Participating Frequently
Oct 1, 2021 10:18 PM UTC

Oh by the way I want to move the player with the wasd keys

Translate