Jump to content

Keyboard Inputs


Karzam
 Share

Recommended Posts

Hi everyone, I'm coding a project and I've got a problem when I try getting keyboard input in a dedicated class. 

 

So this is my Controller class : 

Controller = function() {	// Initialisation clavier 	document.onkeydown = this.onKeyDown;	document.onkeyup = this.onKeyUp;	// Touches 	this.left = false;	this.up = false;	this.down = false;	this.right = false;}// Touche appuyée Controller.prototype.onKeyDown = function(event) {	if (event.keyCode == 37) {		this.left = true;	}	if (event.keyCode == 38) {		this.up = true;	}	if (event.keyCode == 39) {		this.down = true;	}	if (event.keyCode == 40) {		this.right = true;	}}// Touche relâchée Controller.prototype.onKeyUp = function(event) {	if (event.keyCode == 37) {		this.left = false;	}	if (event.keyCode == 38) {		this.up = false;	}	if (event.keyCode == 39) {		this.down = false;	}	if (event.keyCode == 40) {		this.right = false;	}}

... with which I create an occurence during initialization. But when I try to get boolean state in an other class : 

// DéplacementPlayer.prototype.move = function() {     if (controler.left) {        this.posHorizontal -= this.speed;    }}

this isn't working ! When I display the state in the controller class it return 'true' but not in another classes. I'v got no error but only a 'false' displayed (i've tried with console.log but no way). 

 

Thanks for help ! 

Link to comment
Share on other sites

You call your onKeyDown/Up functions as methods of the document-object, so the this inside them will refer to the document object (document.left is true, but not controler.left)

 

So simply reply replace line 2 and 3 with:

document.onkeydown = this.onKeyDown.bind(this);document.onkeyup = this.onKeyUp.bind(this);

The bind-method simply ensures this refers to your instance.

I would also recommend to use window.addEventListener, because if you want to use multiple instances of Controller later you get into trouble, because the instances override document.onkeydown

window.addEventListener("keydown", this.onKeyDown.bind(this), false);window.addEventListener("keyup", this.onKeyUp.bind(this), false);

I would recommend to read some articles about "this" in javascript, so you know exactly what this all means :) In javascript "this" is not defined by declaring a function, but by the way you invoke a function.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...