Skip to content

Instantly share code, notes, and snippets.

@danjordan
Created August 7, 2012 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjordan/ff72beb8e26896757927 to your computer and use it in GitHub Desktop.
Save danjordan/ff72beb8e26896757927 to your computer and use it in GitHub Desktop.
var Runner = function() {
this.canvas = document.getElementsByTagName('canvas')[0];
this.event = document.createEvent('Event');
this.eventType = 'keydown';
this.keys = {
left: 37,
right: 39,
up: 38
};
this.key = 'right';
this.event.initEvent(this.eventType, true, true);
};
Runner.prototype.run = function() {
this.key = (this.key === 'right') ? 'left' : 'right';
this.action();
};
Runner.prototype.jump = function() {
this.key = 'up';
this.action();
};
Runner.prototype.action = function() {
this.event.keyCode = this.keys[this.key];
this.canvas.dispatchEvent(this.event);
};
var runner = new Runner();
setInterval(function() {
runner.run();
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment