Typescript extends keyword not working

2019-09-11 00:18发布

问题:

I'm developing my first game using Visual Studio, Phaser and Typescript.

I can't get my classes to work when I use the extends key word

This works:

class Game  {

game: Phaser.Game;

constructor() {
    // init game
    this.game = new Phaser.Game(window.innerWidth * window.devicePixelRatio - 20, window.innerHeight * window.devicePixelRatio - 20, Phaser.CANVAS, 'content', State);
}
}

This does not:

class Game extends Phaser.Game{

constructor() {
    // init game

    super(window.innerWidth * window.devicePixelRatio - 20, window.innerHeight * window.devicePixelRatio - 20, Phaser.CANVAS, 'content', State);
}
}

I've been trying to figure this out all day without success, can anybody shed some light on it?

回答1:

The script tag for phaser.js needs to be above the script tag for your script.

Each script runs in order and your second example has an immediate dependency on the Phaser object already being created as soon as it runs.