我在这里做一个简单的示例,其中我使用了Phaser库。
module GameName {
class GameName extends Phaser.Game{
constructor(width?:number, height?:number){
super(width, height, Phaser.AUTO, 'PhaserDemo', {create:this.create});
}
create() {
this.state.add("Preloader", Preloader, true);
}
}
window.onload = () => {
new GameName(1280, 720);
}
}
所以我在这里{create:this.create}在“ this”关键字上编译Typescript时遇到错误。
我不明白我在做什么错。我只是将Phaser.Game类的构造函数调用到我的GameName类的构造函数中,然后将Phaser.Game类的create函数作为参数添加到超级构造函数中。
错误:在派生类的构造函数中访问“ this”之前,必须先调用“ super”。
尝试将this.create
函数包装在匿名函数中
constructor(width?:number, height?:number){
super(width, height, Phaser.AUTO, 'PhaserDemo', {
create: () => this.create(),
});
}
this
(?)的立即调用this
在创建回调中可能未定义的修复程序,请参见:https : //github.com/Microsoft/TypeScript/wiki/%27this%27-in-TypeScript本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句