Jump to content

Extending GameObjects?


Meowts
 Share

Recommended Posts

Hola, old time Phaser 2 user, new time Phaser 3 user, I'm digging the changes!

I find Scene management interesting by "extending" Phaser.Scene. I remember creating a whole abstraction for Scene management using Phaser 2, glad to see a much simpler solution!

Just wondering if you can similarly extend say a GameObject, or a particular like Phaser.GameObjects.Line or Sprite or whatever. Pooling objects and letting the Scene just call update() etc on the pool is something I've also worked out in Phaser 2 (abstractions), but having a GameObject with properties and custom update routines etc that has Phaser baked right into it would be a real bonus! I used to pass around the game instance like it was a hot commodity.

Here's where I started, I suppose I would just need help initializing it, or?

var YourLine = new Phaser.class({
    Extends: Phaser.GameObjects.Line,

    initialize : function YourLine () {
        // Something similar to Phaser.Stage.call()? 
    }
});

Many thanks in advance,
Ben

Link to comment
Share on other sites

class MySprite extends Phaser.GameObjects.Sprite {

    constructor (scene, x, y,myExtra)
    {
        super(scene, x, y);
        this.myExtra = myExtra;
        this.setTexture('cachedtexturekey');
        this.setPosition(x, y);
    }
    preload()
    {

    }
    create()
    {

    }
    preUpdate (time, delta)
    {
      // do stuff with this.myExtra
    }

}
5 hours ago, Meowts said:

Just wondering if you can similarly extend say a GameObject, or a particular like Phaser.GameObjects.Line or Sprite or whatever.

Yes you can example above. Those 3 functions overload function of the baseclass GameObject that are called at various points of the object lifecycle. You could Also overload Sprite specific methods like preUpdate if you needed to. All and all it is just javascript and you can do what you want with it. The extended class istance can be pooled too

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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