-
-
Save danjordan/ff72beb8e26896757927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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