ArmTwo

Weapon plugin not run update (Typescript)

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 :-)

Share this post


Link to post
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(/*…*/);

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.