gpt4 book ai didi

javascript - JS 继承和 Sprites 错误

转载 作者:行者123 更新时间:2023-11-30 17:48:16 24 4
gpt4 key购买 nike

所以我正在尝试一起学习 javascript 和 easeljs 来制作一款 TD 游戏。我可以弄清楚如何从教程中扩展 sprite 类以分别制作每个游戏对象。不过,我想做的是创建一个 Sprite 基类,然后每个对象(例如 TowerEnemy )都将从它继承.

实体.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'.

最佳答案

替换:

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

关于javascript - JS 继承和 Sprites 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com