【问题标题】:Phaser text event not recognizing listener functionPhaser 文本事件无法识别侦听器功能
【发布时间】:2018-06-17 06:57:12
【问题描述】:

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

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

这是整个打字稿减去创建 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)

【问题讨论】:

    标签: typescript phaser-framework


    【解决方案1】:

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

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多