createFromObjects classType and typescript extends

Hi,

I want to use CreateFromObjects to create Players, which are custom classes that extend the Phase.GameObjects.Sprite object.

I thought I could achieve this using the the classType property. But I get the following error:

TS2740: Type 'typeof Player' is missing the following properties from type 'GameObject': scene, displayList, type, state, and 42 more.

( I’m using typescript )

Here is example code:

import * as Phaser from 'phaser';

class Player extends Phaser.GameObjects.Sprite {
    constructor(scene: Phaser.Scene, x: number, y: number) {
        super(scene, x, y, "player_blue");
    }
}

const sceneConfig: Phaser.Types.Scenes.SettingsConfig = {
    active: false,
    visible: false,
    key: 'Game',
};

export class GameScene extends Phaser.Scene {
    private player: Player;

    constructor() {
        super(sceneConfig);
    }

    public preload() {
        this.load.image("tiles_board", "/assets/tilesets/board.png");
        this.load.image("player_blue", "/assets/sprites/player_blue.png"); 
        this.load.tilemapTiledJSON('map_test', '/assets/maps/test.json');
    }

    public create() {

        const map = this.make.tilemap({ key: 'map_test' });
        const tiles = map.addTilesetImage("tiles_board");
        map.createLayer('main', tiles);


        const players: Player[] = <Player[]>map.createFromObjects('players', {
            name: 'player1',
            classType: Player
        });
        this.player = players[0];

     

    }
   
}

I feel like I’m missing something fundamental about how typescript inheritance works, but I can’t see what. Could anyone point out my mistake?

Thanks!

That looks correct. Can you try { classType: Phaser.GameObjects.Sprite } to see what happens?

Good thinking, I get a similar error when using that:

TS2740: Type 'typeof Sprite' is missing the following properties from type 'GameObject': scene, displayList, type, state, and 42 more.

Does that suggest I’ve got the syntax for setting the property wrong? For completeness, the call to createFromObjects now looks like this:

  const players: Player[] = <Player[]>map.createFromObjects('players', {
            name: 'player1',
            classType: Phaser.GameObjects.Sprite
        });

This may have been a problem with Phaser’s types. You could update Phaser or just ignore the error.

1 Like

Ah ha! I just learnt that @ts-ignore exists. Ignore the error on that line allows my code to run.
So, as you say, it’s more likely a problem with Phaser’s types. This is on latest release (v3.55.2).

Looks like supporting classTypes in createFromObjects only came in v3.50.0 (Dec 2020): Support classType in CreateFromObjects/Tiles · Issue #4613 · photonstorm/phaser · GitHub .

Thanks for your help.