私はこの仕事をするために髪を抜いてきました...誰かがそれの何が悪いのか知っていますか?
テキストオブジェクトをボタンとして使用しようとしていますが、ゲームが起動して黒い画面が表示され、エラーが発生します。
これは、タイプスクリプト全体からwindow.onload
MyGameインスタンスを作成するハンドラーを除いたものです。
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
、クラスインスタンスではなく、指定したオブジェクトが含まれます。(this
理由を知りたい場合は、どのように機能するかを調べてください。)
onPlay
そのオブジェクトに存在しないので、this.onPlay
となりますundefined
ので、フェイザーは、あなたがそれをリスナーのparamを与えていないと思うだろう。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加