我正在嘗試在使用打字稿(pixi.js)制作的游戲中實作繼承,但我遇到了這個錯誤。我有一個類 Dog,代碼如下:
export class Dog extends Enemy {
//private game: Game;
//private speed: number = 0;
constructor(texture: PIXI.Texture, game: Game) {
super(texture);
this.game = game;
}
我還有一個類敵人,類狗擴展到它,代碼如下:
export class Enemy extends PIXI.Sprite {
public game: Game;
public speed: number = 0;
constructor(texture: PIXI.Texture, game: Game) {
super(texture);
this.game = game;
}
但是在我的 dog.ts 檔案中,我在 super(texture); 下收到以下錯誤。我的代碼的一部分:預期 2 個引數,但得到 1.ts(2554)enemy.ts(8, 38):未提供“游戲”的引數。
我不知道這意味著什么,因為我希望我已經在enemy.ts 中為游戲提供了一個論據,并且我也擴展到了 Pixi.sprite。
編輯:如果我將enemy.ts 中的 super 更改為 super(texture, game) 我會收到以下錯誤:如果我這樣做我會收到以下錯誤:Expected 0-1 arguments, but got 2.ts(2554)
我只能在超級函式中加上引數,所以恐怕這不能解決問題
編輯 2:Nvm 我更改了錯誤的檔案
uj5u.com熱心網友回復:
因為您沒有game在super. 在Dog類中向函式添加game引數super:
super(texture, game);
uj5u.com熱心網友回復:
好吧,建構式Enemy期望輸入中有兩個引數:texture: PIXI.Texture, game: Game但您只給出第一個引數。
所以,在你的dog.ts從
super(texture)
修改為
super(texture, game)
Pixi.Sprite如果需要相同的兩個引數,則 Enemy 類中可能會出現相同的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/489730.html
標籤:javascript 打字稿
