Skip to content

Instantly share code, notes, and snippets.

@justinmc
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save justinmc/7c33d43c645baeb23c16 to your computer and use it in GitHub Desktop.

Select an option

Save justinmc/7c33d43c645baeb23c16 to your computer and use it in GitHub Desktop.
ES6 Generator Example: Game Dialog
class Game {
constructor() {
...
// kick off our generator game flow
this.flowGen = this.flow();
this.flowGen.next();
...
}
...
*flow() {
this.setTextbox('Hello! Click to continue!');
yield this.events.once('click', () => {
this.setTextbox('I\'m just an ordinary game dialog system.');
this.flowGen.next();
});
yield this.events.once('click', () => {
this.setTextbox('But I use ES6 generators, which is cool I guess.');
this.flowGen.next();
});
yield this.events.once('click', () => {
this.setTextbox('Anyway enough about me. Do you like (r)ed or (b)lue more? (Type the letter)');
this.flowGen.next();
});
this.events.once('keyup 82', () => {
this.setTextbox('OMG I love red!');
this.stage.setBackgroundColor(0xff5555);
this.flowGen.next();
});
this.events.once('keyup 66', () => {
this.setTextbox('Yeah whatever blue\'s ok.');
this.stage.setBackgroundColor(0x5555ff);
this.flowGen.next();
});
yield this.events.once('click', () => {
this.setTextbox('I said type, not click. R or B.');
});
yield this.events.once('click', () => {
this.setTextbox('Welp that\'s all! You can refresh if you want to start over.');
this.stage.setBackgroundColor(0x000000);
this.flowGen.next();
});
yield this.events.once('click', () => {
this.removeTextbox();
});
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment