I have an object called TruckModel which is defined earlier in my JavaScript file called 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.
I know that my current code isn’t working because the model won’t display when I use the teleportToThat function below.
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
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)
Declared in index.html file:
var model;
Declared in JavaScript file:
model.teleportToThat( lat, lng, heading );
Here’s the entire JavaScript file:
If you want to be able to find the objects by numeric index, you want an array, not a plain object:
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:
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.