我还是相当新的原型继承在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