Skip to content

Instantly share code, notes, and snippets.

@manaten
Created April 30, 2013 05:34
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 manaten/5486754 to your computer and use it in GitHub Desktop.
Save manaten/5486754 to your computer and use it in GitHub Desktop.
Generatorでこんなことが出来ればいいのに、という夢
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