I'm trying to write a simple for loop inside of an object. Essentially what it is doing is setting the Z position of the object before it plus delta, with the exception that the 0th object simply adds delta to it's original position. Below is the code I am using to do this:
this.objs[0].setZ(this.objs[0].getZ()+delta);
var i;
for (i = 0; i < this.n; i++) {
this.objs[i+1].setZ(this.objs[i].getZ()+delta);
alert("i = " + i + " i+1 =" + i+1);
}
For some reason the alert returns i = 0 i+1 = 01 on the first pass and i = 1 i+1 = 11 on the second pass. This seems like it is treating i as a string or something, since I would expect the alert to print out i = 0 i+1 = 1 on the first pass and i = 1 i+1 = 2 on the second.
I think this seems to be confirmed by the following TypeError I get in when I run the code in Firefox Development Tools.
TypeError: this.objs[(i + 1)] is undefined
What exactly am I doing wrong here that i+1 is the wrong type?
----EDIT----
I noticed the for loop was going out of bounds so I changed it to this:
for (i = 1; i < this.n; i++) {
this.objs[i].setZ(this.objs[i-1].getZ()+delta);
alert(this.n);
}
}
(the value of this.n = 3). For some reason this keeps going through the loop when i=3. I have no idea why..