Phaser 文本事件无法识别侦听器功能

Phaser text event not recognizing listener function

我一直在努力使这项工作成功...有人知道它有什么问题吗?

我正在尝试将文本对象用作按钮,但游戏启动时黑屏并出现错误。

这是整个打字稿减去创建 MyGame 实例的 window.onload 处理程序。

class MyGame {

game: Phaser.Game;
textStyle: object = { font: "Ubuntu", fill: "black", align: "center" };

constructor() {
    this.game = new Phaser.Game(800, 640, Phaser.AUTO, 'content', { preload: this.preload, create: this.create, update: this.update });
}

preload() { }

create() {

    this.game.stage.backgroundColor = "#eee";

    let buttonPlay = this.game.add.text(this.game.world.centerX, this.game.world.centerY, "play", this.textStyle);
    buttonPlay.anchor.setTo(0.5);
    buttonPlay.inputEnabled = true;
    buttonPlay.events.onInputUp.add(this.onPlay, this); //line 19

}

update() { }

onPlay() {
    console.log("pressed play");
}
}

这是我得到的错误:

Uncaught Error: Phaser.Signal: listener is a required param of add() and should be a Function.
at i.Signal.validateListener (phaser.min.js:3)
at i.Signal.add (phaser.min.js:3)
at Object.MyGame.create (app.ts:19)

不要在构造函数中创建游戏实例,而是像这样在 onload 处理程序中创建游戏实例:

new Phaser.Game(800, 640, Phaser.AUTO, 'content', new MyGame());

Phaser 会在您提供的对象上调用 create 方法:

{ preload: this.preload, create: this.create, update: this.update }

在方法内部,this 将成为您提供的对象,而不是 class 实例。 (如果您想知道原因,请查看 this 的工作原理。)

onPlay 在那个对象上不存在,所以 this.onPlay 将是 undefined 因此 Phaser 会认为你没有给它一个监听器参数。