JS继承和Sprites出错

时间:2013-10-29 09:56:08

标签: javascript easeljs

所以我试图一起学习javascript和画架来制作TD游戏。我可以解决如何从教程扩展sprite类以分别制作每个游戏对象。我想做的是创建一个Sprite的基类,然后TowerEnemy等每个对象都会继承它。

Entity.js

function Entity(name, img, x_end) {
   this.initialize(name,img,x_end); <- throws Error 
}

Entity.prototype = new createjs.Sprite();
Entity.prototype.Sprite_initialize = this.initialize; //unique to avoid overiding base class

Entity.prototype.initialize = function (name, img, x_end) {
    var localSpriteSheet = new createjs.SpriteSheet({
        images: [img], //image to use
        frames: {width: 32, height: 32},
        animations: {
            walk: [0, 0, "walk", 4],
        }
    });

    this.Sprite_initialize(localSpriteSheet);
    this.x_end = x_end;

    // start playing the first sequence:
    this.gotoAndPlay("walk");     //animate

    // starting directly at the first frame of the walk_h sequence
    this.currentFrame = 0;
};

Tower.js

function Tower(TowerName, imgTower, x_end) {

    Entity.call(this,arguments);
}

//Inherit Entity
Tower.prototype = new Entity();

// correct the constructor pointer because it points to Person
Tower.prototype.constructor = Tower;

Main.js

var Towers = new Array();
Towers[0] = new Tower("TowerA", "src/images/arrowtower_thumb2.png", canvas.width)

错误

Uncaught TypeError: Object #< Tower > has no method 'initialize'.

1 个答案:

答案 0 :(得分:2)

替换:

Entity.prototype.Sprite_initialize = this.initialize;

with:

Entity.prototype.Sprite_initialize = Entity.prototype.initialize;

并在Tower.js中添加“initialize”方法

Tower.prototype.Tower_initialize = Tower.prototype.initialize; 
Tower.prototype.initialize = function () {
   ...
}
相关问题