Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.