[英]What's wrong with how I'm declaring this Javascript variable?
I have an object called TruckModel which is defined earlier in my JavaScript file called milktruck.js我有一个名为 TruckModel 的 object,它在前面的 JavaScript 文件中定义,名为 milktruck.js
I'm trying to create an array of these TruckModel objects because I don't know at any given time how many TruckModel objects will be needed as players of my multiplayer game enter and exit.我正在尝试创建这些 TruckModel 对象的数组,因为在任何给定时间,当我的多人游戏的玩家进入和退出时,我都不知道需要多少 TruckModel 对象。
I know that my current code isn't working because the model won't display when I use the teleportToThat function below.我知道我当前的代码不起作用,因为当我使用下面的 teleportToThat function 时,不会显示 model。
I was able to get the model to display by declaring only one TruckModel() object in my index.html file and then using the teleportToThat我能够通过在我的 index.html 文件中仅声明一个 TruckModel() object 然后使用teleportToThat
Here's my code for this, do you see any errors in how I'm doing it?这是我的代码,您在我的操作中看到任何错误吗?
Non-working version:非工作版本:
var opponentTrucks = [];
for (var i = 0; i < markers.length; i++) {
opponentTrucks[i] = new TruckModel();
opponentTrucks[i].teleportToThat( lat, lng, heading );
}
Working version: (Difference being that I'm trying to have a varying amount of TruckModel objects)工作版本:(不同之处在于我试图拥有不同数量的 TruckModel 对象)
Declared in index.html file:在 index.html 文件中声明:
var model;
Declared in JavaScript file:在 JavaScript 文件中声明:
model.teleportToThat( lat, lng, heading );
Here's the entire JavaScript file:这是整个 JavaScript 文件:
http://thehobbit2movie.com/milktruck.js http://thehobbit2movie.com/milktruck.js
If you want to be able to find the objects by numeric index, you want an array , not a plain object:如果您希望能够通过数字索引找到对象,则需要一个数组,而不是普通的 object:
var opponentTrucks = [];
What you've got will work, kind-of, but there's no reason not to use a real array if you're going to treat it like one anyway.你所拥有的东西会起作用,但如果你想把它当作一个真正的数组来对待,没有理由不使用它。
edit — it's still not really clear exactly what the problem is.编辑-仍然不清楚到底是什么问题。 This line here:
这一行在这里:
opponentTrucks[i].teleportToThat( lat, lng, heading );
What is that supposed to do?那应该做什么? Where is it called from?
它是从哪里调用的? What's the value of "i"?
“我”的价值是什么? If you simply have that statement following the loop, then it's not going to work.
如果你只是在循环之后有那个语句,那么它就行不通了。 If you want to have that "teleportToThat()" function called for each one in the array , then you should put the function call inside the "for" loop.
如果你想为数组中的每一个调用“teleportToThat()”function,那么你应该把 function 调用放在“for”循环中。
If you're only using numeric keys, you want an array, rather than an object:如果您只使用数字键,则需要一个数组,而不是 object:
var opponentTrucks = [];
for (var i = 0; i < markers.length; i++) {
opponentTrucks.push(new TruckModel());
}
This probably wouldn't stop your code actually working, but it would almost certainly be an improvement.这可能不会阻止您的代码实际工作,但它几乎肯定会是一种改进。
If there are still errors, perhaps you could say what they are:-)如果仍然有错误,也许您可以说出它们是什么:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.