2017-02-16 64 views
0

我正在做一個簡單的例子,在這裏我使用Phaser庫。 {:this.create創建}Typescript - 「this」關鍵字錯誤

我無法理解我在做什麼錯

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); 
    } 
} 

所以我同時對「this」關鍵字這裏編譯打字稿得到一個錯誤。我只是調用Phaser.Game類的構造函數到我的GameName類的構造函數中,並且將Phaser.Game類的create函數作爲參數添加到超級構造函數中。

錯誤:必須在派生類的構造函數中訪問'this'之前調用'super'。 (?)

回答

1

嘗試在一個匿名函數

constructor(width?:number, height?:number){ 
    super(width, height, Phaser.AUTO, 'PhaserDemo', { 
    create:() => this.create(), 
    }); 
} 
+0

是的,這樣錯誤消失了,它編譯,但它根本沒有進入函數。 但是,如果我做這種方式 - > 構造(寬度:編號,高度:數?){ 超(寬度,高度,Phaser.AUTO, 'PhaserDemo',{創造:()=> { this.state.add(「Preloader」,Preloader,true); }}); } } 它工作得很好。 –