Jump to content

inheritance in phaser 3


mapacarta
 Share

Recommended Posts

I am trying to inherit Phaser classes, here is what I have done so far:

class Obj extends Phaser.GameObjects.Image {

    constructor (scene, x, y)
    {
        super(scene, x, y, 'object');
        scene.add.displayList.add(this);
    }

}

 

Is this a proper way to inherit a class?

Also is it possible to use prototypal inheritance in phaser 3 like we do in phaser 2?

Link to comment
Share on other sites

How about arcade sprites? When I extends physics.sprite the object doesn't get a body, acts like a normal sprite.

I tried to add it to the physics world like this:

this.physics.world.enableBody(robot,CONST.DYNAMIC_BODY);

it gives an error of course since CONST is not defined and I couldn't find what CONST.DYNAMIC_BODY equals to. "dynamic" or "dynamicBody" maybe :)

-EDIT-

Ok I found it CONST.DYNAMIC_BODY stands for 0

When I do

this.physics.world.enableBody(robot,0);

it gets a body.

-ANOTHER EDIT-

the object also didn't added to the updateList. I don't know what updateList does but do we need to manually add it to the updateList too?

this.sys.updateList.add(robot);

Edited by mapacarta
Link to comment
Share on other sites

  • 4 months later...

I still couldn't find how to add a custom arcade sprite properly :) I am just using some workarounds and stuffs like that. Here is what I am currently using:

var customObject = new Phaser.Class({

    Extends: Phaser.Physics.Arcade.Sprite,

    initialize:

    function customObject (scene, x, y)
    {
        Phaser.Physics.Arcade.Sprite.call(this, scene);
        scene.physics.world.enableBody(this,0);
        scene.sys.updateList.add(this);
        
        this.setTexture('object');
        this.setPosition(x, y);
   
    }

});

Is this the right way to add inheritence or is there any better way?

Link to comment
Share on other sites

Honestly, I would use ES6. However if we do it your way, you can save yourself quite a bit of hassle by using the whole of the parent constructor. I think (not sure) that it's common policy to add  some code so that your sprite is auto-loaded into the scene when constructed. 

 

var CustomObject = new Phaser.Class({

    Extends: Phaser.Physics.Arcade.Sprite,

    initialize:

    function CustomObject (scene, x, y)
    {
        Phaser.GameObjects.Sprite.call(this, scene, x, y, 'object');

        scene.physics.add.existing(this);
    }
});

I would also use the GameObject version of Sprite rather than the Physics.Arcade version, but I don't actually know the difference between the two!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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