1

所以我正在尝试一起学习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'.
4

1 回答 1

2

代替 :

Entity.prototype.Sprite_initialize = this.initialize;

和 :

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

并在 Tower.js 中添加“初始化”方法

Tower.prototype.Tower_initialize = Tower.prototype.initialize; 
Tower.prototype.initialize = function () {
   ...
}
于 2013-10-29T10:16:33.150 回答