Jump to content

Weapon plugin not run update (Typescript)


ArmTwo
 Share

Recommended Posts

Hi,

I'm writing a class that extends Phaser.Weapon and i want that the method update() keep running, like this:

class MissileWeapon extends Phaser.Weapon {
  constructor(game: Phaser.Game){
    super(game, game.plugins);
  }

  update(){
    /*this not execute*/
  }
}

But it not running.

If i add this "hack":

class MissileWeapon extends Phaser.Weapon {
  constructor(game: Phaser.Game){
    super(game, game.plugins);

    /*the hack*/
    this.active = true; /*active plugin*/
    this.hasUpdate = true;
    var test = game.plugins.add(Phaser.Weapon); /*Only for the game.plugins(PluginManager) start running*/
    game.plugins.plugins.push(this); /*add my weapon in the PluginManager*/
  }

  update(){
    /*executing */
  }
}

It seems that when extends of plugin Weapon, it not is added direct in the game.plugins (PluginManager), then the method update not executes. 

How could i make this without uses the "hack"?

and i'm using Phaser version 2.6.2

thanks :-)

Link to comment
Share on other sites

Weapons (and all Plugins) instantiate themselves but it's the PluginManager's job to add and activate them. You can imitate game.add.weapon:

Phaser.GameObjectFactory.prototype.missileWeapon = function (quantity, key, frame, group) {
  var weapon = this.game.plugins.add(MissileWeapon);
  weapon.createBullets(quantity, key, frame, group);
  return weapon;
};

// …

game.add.missileWeapon(/*…*/);

 

Link to comment
Share on other sites

thanks @samme, works like a charm.

I just has needed of add the method init(), because the constructor of the MissileWeapon receive two args.

before:

class MissileWeapon extends Phaser.Weapon {
  someVar: string;
  constructor(game: Phaser.Game, someVar: string){
    super(game, game.plugins);
    this.someVar = someVar;

    /*the hack*/
    this.active = true; /*active plugin*/
    this.hasUpdate = true;
    var test = game.plugins.add(Phaser.Weapon); /*Only for the game.plugins(PluginManager) start running*/
    game.plugins.plugins.push(this); /*add my weapon in the PluginManager*/
  }
}

//level01.ts
this.myMissileWeapon = new MissileWeapon(this.game, "something");

now:

class MissileWeapon extends Phaser.Weapon {
  someVar: string;

  constructor(game: Phaser.Game, parent: Phaser.PluginManager){
    super(game, game.plugins);
  }  

  init(someVar: string){
    this.someVar = someVar;
  }
}

//level01.ts
this.myMissileWeapon = this.game.plugins.add(MissileWeapon, "something");

 

Edited by ArmTwo
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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