Skip to content

Instantly share code, notes, and snippets.

@timkim
Created November 15, 2011 22:35
Show Gist options
  • Save timkim/1368596 to your computer and use it in GitHub Desktop.
Save timkim/1368596 to your computer and use it in GitHub Desktop.
Quick fix for scope
// Try setting var self = this in the init and using self.mouseX/self.mouseY in onMouseMove
GAME.Input = function ( parent ) {
this.game = parent;
this.oldMouseX = 0;
this.oldMouseY = 0;
this.mouseX = 0;
this.mouseY = 0;
};
GAME.Input.prototype = {
init: function ( useMouse, useKeyboard ) {
var self = this;
if ( useMouse == true) {
document.addEventListener( 'mousemove', this.onMouseMove, false );
this.write("Mouse move listener started");
}
},
update: function () {
if (this.mouseX != this.oldMouseX || this.mouseY != this.oldMouseY) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.write(this.mouseX + " x " + this.mouseY);
}
},
onMouseMove: function ( event ) {
self.mouseX = event.clientX;
self.mouseY = event.clientY;
},
write: function ( info, color ) {
this.game.config.write( 'Input > ' + info, color );
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment