A.Russell Posted May 10, 2015 Share Posted May 10, 2015 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 More sharing options...
XekeDeath Posted May 10, 2015 Share Posted May 10, 2015 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 More sharing options...
A.Russell Posted May 11, 2015 Author Share Posted May 11, 2015 Thanks Xeke Death. add.existing was was I needed. Link to comment Share on other sites More sharing options...
Recommended Posts