Home > Software design >  Phaser3 and Typescript - Extends SpriteWithDynamicBody
Phaser3 and Typescript - Extends SpriteWithDynamicBody

Time:12-31

I'd like to create a class hero that extends from SpriteWithDynamicBody but I can't since SpriteWithDynamicBody is only declared as a type (and we can't extend from a type in typescript)

I can only extends Sprite but then, some methods/properties are not available:

export class Hero extends Phaser.GameObjects.Sprite {
    constructor(public scene: Phaser.Scene, x: number, y: number, texture: string, frame?: number) {
        super(scene, x, y, texture, frame);
        this.setVelocity(0); // => Error
    }
}

What can I do to extend SpriteWithDynamicBody ?

CodePudding user response:

You can extend from the class Phaser.Physics.Arcade.Sprite, if you need to extend a Sprite with a dynamicBody. Link to the documentation

Here is a link to a demo from the official website.

  • Related