简体   繁体   English

我如何声明这个 Javascript 变量有什么问题?

[英]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.

相关问题 此var声明在名称中声明带点的变量有什么问题? - What's wrong with this var statement declaring a variable with a dot in the name? 此JavaScript代码有什么问题? 我正在尝试更改的文本 <h1> 点击元素 - What's wrong with this JavaScript code? I'm trying to change the text of <h1> element on click 我在javascript函数上传递了三个3参数,但是它只能得到第一个参数,这可能是错误的 - I'm passing three 3 parameter on a javascript function but it only get's the first parameter what could be wrong 我正在尝试创建一个私有变量。 该函数应该返回私有变量 &#39;secret&#39; 的值,但事实并非如此。 怎么了? - I'm trying to create a private variable. The function was supposed to return the value of the private variable 'secret', but it's not. What's wrong? javascript - 首先声明变量函数的目的是什么? (两次声明变量) - javascript - what is the purpose of declaring a variable a function first? (declaring variable twice) 我正在尝试使用 javascript 创建一个绘图应用程序,但它应该发生的 canvas 元素没有响应。 我的代码有什么问题? - I'm trying to create a drawing app with javascript, but the canvas element it's supposed to take place in is unresponsive. What's wrong with my code? 将Python代码实现为Javascript时,我在做什么错? - What i'm doing wrong when implementing Python code to Javascript? 这个实现有什么问题? 我无法覆盖样式 - What's wrong with this implementation? I'm unable to override the style Javascript 变量未在函数的 Scope 中声明 - Javascript Variable Not Declaring in Function's Scope 这些javascript有什么问题 - What's wrong with these javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM