所以我正在尝试一起学习javascript和easeljs来制作一个TD游戏。我可以研究出如何从教程中扩展精灵类以分别制作每个游戏对象。不过,我想做的是创建一个基类,Sprite
然后每个对象(例如Tower
,Enemy
都将从它继承)。
实体.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;
主.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'.