2013-10-29 53 views
1

所以我想學習JavaScript和easeljs一起做一個TD遊戲。我可以計算出如何從教程擴展精靈類來分別製作每個遊戲對象。我試圖做的是做一個基類,它是一個Sprite,然後每個對象如Tower,Enemy將從它繼承。JS繼承和Sprites錯誤

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) 

錯誤

​​3210

回答

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() { 
    ... 
} 
+0

添加一個 「初始化」 的方法並沒有幫助。同樣的錯誤。 – BrainPicker