Skip to main content
sammyueru
Participating Frequently
October 1, 2021
Answered

Can't move with scripts - HTML5

  • October 1, 2021
  • 2 replies
  • 346 views

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 has been closed for replies.
Correct answer JoãoCésar17023019

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

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
October 1, 2021

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

sammyueru
sammyueruAuthor
Participating Frequently
October 1, 2021

that worked thank you so much

JoãoCésar17023019
Community Expert
Community Expert
October 2, 2021

You're welcome!

sammyueru
sammyueruAuthor
Participating Frequently
October 1, 2021

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