Created
April 30, 2013 05:34
-
-
Save manaten/5486754 to your computer and use it in GitHub Desktop.
Generatorでこんなことが出来ればいいのに、という夢
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 Sprite = function(){ | |
this.x = 0; | |
this.v = 0.5; | |
}; | |
// x方向に引数で与えた分だけ速度vで進む | |
Sprite.prototype.move = function(x) { | |
var time = x / this.v; | |
for (var i = 0; i < time; i++) { | |
this.x += this.v; | |
yield; | |
} | |
}; | |
var sprites = []; | |
var sprites[0] = new Sprite(); | |
sprites[0].run = function() { | |
this.move(100); | |
this.move(10); | |
}; | |
var sprites[1] = new Sprite(); | |
sprites[1].run = function() { | |
this.move(10); | |
this.move(50); | |
}; | |
var tasks = []; | |
sprites.forEach(function(sprite) { | |
tasks.push(new (sprite.run())); | |
}); | |
// Game main loop | |
window.setInterval(function() { | |
tasks.forEach(function(task) { | |
task.next(); | |
}); | |
}, 16); // 60FPS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment