2

我来自 C#/Java 背景,我正在努力学习如何用 HTML5 制作游戏。我遇到了一个令人难以置信的问题,虽然通常我会问一个朋友,但他一直是 MIA。我创建了一个帐户来向社区提问,我感到很难过,但我一直在寻找答案,但无济于事。

所以问题是......我想创建一个可以容纳塔实例的游戏对象。另外,我想创建一个塔的数组列表作为一种后端存储库。本质上,我希望这些是我能够在我的主游戏中引用的外部 js 文件。我不确定这在 js 中是否可行或如何工作。

我试图做的是......创建一个外部JS文件并声明一个塔对象。这就是我迷路的地方。我想创建另一个对象,它只是第一种类型的列表,即 towerList。在这个外部 JS 文件中,我打算用一堆塔属性填充列表。然后,在带有 canvas 元素的主文件中,我将遍历列表并绘制图形。我不知道该怎么做。

function Tower (type, texture, cost, damage, speed, range, health, Xpos, Ypos) 
{
this.type = type;
this.texture = new Image();
this.texture.src = texture;
this.cost = 0;
this.damage = 0;
this.speed = 0;
this.range = 0;
this.health = 0;
this.Xposition = 0;
this.Yposition = 0;
this.target = false;
}


var TowerList = [Tower("Basic", "tower.png", 0, 0, 0, 0, 0, 0, 0), Tower(), Tower()];

我怀疑我的工作是否正常,但我希望能够在主项目中创建 towerlist 的实例,像数组一样随机访问它,并按照我认为合适的方式打印它的内容。在 JS 中实现这一目标的最佳 OO 方式是什么?我希望稍后在主代码中能够从列表中分配一个塔

var tL = new TowerList();
var tower = new Tower();

tower = tL[0];

我今天才开始做这个,所以我意识到要学习很多东西。我想我需要将函数重新定义为 var 否则我可以实例化它(我确定我之前读过)。如果有人可以帮助我或为我指明一个资源的方向,其中包含我可以从中学习的示例,我将非常感激。

4

3 回答 3

1

TowerList不必是新类型的对象,只需Array要做:

var towerList = [];
towerList.push(new Tower("Basic", "tower.png", 0, 0, 0, 0, 0, 0, 0));
//             ^^^
//            use 'new' to instantiate a new Tower
towerList.push(...);

迭代TowerList

var tower;

for (var i = 0, n = towerList.length; i < n; ++i) {
    tower = towerList[i];
    // do stuff with tower
}

您也可以TowerList继承自Array

function TowerList()
{
}

TowerList.prototype = [];

TowerList.prototype.yourFunction = function() {
}

var towerList = new TowerList();

towerList.push(new Tower(...));

towerList.yourFunction();
于 2013-04-08T03:38:56.810 回答
0

我可能会让塔返回一个对象,然后创建它的实例。(避免“这个”)

考虑一种模块化方法:这对我有帮助:http: //www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

粗略的想法:

function Tower(type, texture, cost, damage, speed, range, health, Xpos, Ypos)  {
    var obj = {};
        obj.type = type;
        obj.texture = new Image();
        obj.texture.src = texture;
        obj.cost = cost;
        obj.damage = 0;
        obj.speed = 0;
        obj.range = 0;
        obj.health = 0;
        obj.Xposition = 0;
        obj.Yposition = 0;
        obj.target = false;

    return obj;
}


var TowerList = [Tower("Basic", "r", 5, 0, 0, 0, 0, 0, 0), Tower(), Tower()];
var mytower = TowerList[1];
于 2013-04-08T04:00:25.383 回答
0
//your towers should be called using new here, assuming you actually 
//want a list of towers
var TowerList = [Tower("Basic", "tower.png", 0, 0, 0, 0, 0, 0, 0), Tower(), Tower()];
//TowerList needs to be a function to be called with new.  This will error out.
var tL = new TowerList();
//This should work correctly
var tower = new Tower();

//this would overwrite the new Tower() with tl[0]
tower = tL[0];

如果您希望能够创建 TowerList 对象,您可能想要这样的东西

function TowerList(){
    this.list = [];

}


TowerList.prototype.addTower(tower){
  list.push(tower);

}


TowerList.prototype.getList(){
  return this.list;

}

然后你可以这样做

var tL = new TowerList();
tL.addTower(new Tower("Basic", "tower.png", 0, 0, 0, 0, 0, 0, 0));
tL.addTower(new Tower());


var tower = tL[0];
于 2013-04-08T03:31:11.280 回答