Jump to content

ES6 + Phaser is great


eddieone
 Share

Recommended Posts

Here's the scenario, 1 to 5 animals attack the boss, then the boss attacks each animal. This is the code using async/wait

 

await this.attackBoss(this.seat1);
if (this.seats.length >= 2) { await this.attackBoss(this.seat2); }
if (this.seats.length >= 3) { await this.attackBoss(this.seat3); }
if (this.seats.length >= 4) { await this.attackBoss(this.seat4); }
if (this.seats.length >= 5) { await this.attackBoss(this.seat5); }
await this.bossAttack(this.seat1);
if (this.seats.length >= 2) { await this.bossAttack(this.seat2); }
if (this.seats.length >= 3) { await this.bossAttack(this.seat3); }
if (this.seats.length >= 4) { await this.bossAttack(this.seat4); }
if (this.seats.length >= 5) { await this.bossAttack(this.seat5); }

The above code takes a few seconds for all the animations to complete. Using promises to end the await part once the animations finish.

 

attackBoss(seat) {
  return new Promise(resolve => {
    this.tweens.add({
      targets: seat,
      x: this.bossSeat.x,
      ease: 'Sine',
      duration: 500,
      yoyo: true,
      onYoyo: () => {
        this.bouncBoss();
      },
      onComplete: () => {
        resolve('resolved');
      }
    });
  });
}



I might make a goldilocks es6 boilerplate for phaser 3. Mostly just adding async to generator babel plugin to already existing boilerplates. babel-plugin-transform-async-to-generator

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...