Jump to content

How to Create Subclasses of Sprite


Recommended Posts

I have tried to make a subclass of Phaser.Sprite, and I want to add it to the game instance. Here is the class that extends Sprite: 

class Entity extends Phaser.Sprite {        isMoving: Boolean;        constructor(game: Phaser.Game, x: number, y: number, key: string) {            super(game, x, y, key);        }        init(dragable: Boolean = true) {            if (dragable) {                this.inputEnabled = true;                this.events.onEnterBounds.add(this.eventDragStart);            }        }        eventDragStart() {            this.isMoving = true;        }        eventDragStop() {            this.isMoving = false;        }        update() {            if (this.isMoving) {                this.position.set(this.game.input.x, this.game.input.y);            }        }    }

I am unable to add instances to the game.  


I tried:

this.entity = <Entity>this.add.sprite(120, 0, "testSprite");


...and I have a visible sprite, though the constructor for Entity is not called and none of its functions work. Clearly a sprite has been added, but not as this.entity. 


I also tried: 

this.entity = new Entity(this.game, 0, 0, "testSprite");


...The constructor runs, but there is no image.


The image is loaded as: 

this.load.image("testSprite", "../Sprites/Top_1.png");




Link to comment
Share on other sites

Attempt A explicity creates a Sprite object and adds it to whatever display object container 'this' is, that is what this.add.sprite does...

Which would be why you have a sprite on screen of the Sprite type...


Attempt B runs your constructer, but unless you add it to the stage somehow, it won't draw.

Try adding your entity to whatever 'this' is:

this.entity = new Entity(this.game, 0, 0, "testSprite");this.add.existing(this.entity);
Link to comment
Share on other sites


  • Recently Browsing   0 members

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