我還是相當新的原型繼承在JS中,我遇到了一個奇怪的錯誤,我想不出。我們使用畫布編寫遊戲,我們的代碼看起來像這樣:JavaScript - 兒童繼承打破父母
function GameObject() {
// Generic object that everything inherits from
this.x = 0;
this.y = 0;
this.dx = -0.5;
this.dy = 0;
this.width = 0;
this.height = 0;
this.sprite = new Image();
this.sprite.src = "";
}
function Block(x,y) {
this.x = x;
this.y = y;
this.width = 2 * TILE_SIZE;
this.height = 2 * TILE_SIZE;
this.sprite.src = "images/block/block3.png";
blocks.push(this); // Manager array
}
Block.prototype = new GameObject();
Block.prototype.constructor = Block;
此代碼正常工作。請注意,TILE_SIZE是在包含在塊腳本之後的單獨文件中定義的 - 這在這裏似乎不是問題。但是,當我們添加以下代碼(塊功能後):
function Block_Turret(x,y) {}
Block_Turret.prototype = new Block();
Block_Turret.prototype.constructor = Block_Turret;
我得到一個未捕獲的ReferenceError即TILE_SIZE未在阻止功能定義!這似乎是孩子打破了父母,我不知道爲什麼會這樣 - 這似乎是一個相當直接的繼承情況。提前致謝!
之前,您說得對 - 我一切之前,感動了所有配置的東西,它似乎是工作的罰款。排序一個愚蠢的錯誤,哦,好。謝謝! – ark 2012-03-11 23:00:52
@ark,很高興。 – davin 2012-03-11 23:01:33