Quantcast
Channel: Answers for "Array index confusion - what does [i-1] do exactly???"
Viewing all articles
Browse latest Browse all 8

Answer by Jessy

0
0

In short, array[i - i] gives you the value of the element at the index 1 less than i.

Your code does not generate those values. It either generates 40-160, or just 40, for index 1-4 in gearSpeeds. You obviously have something else going on in your code that's altering what you posted.

Anyway, there's no reason you have to start your for loop at 0. You could clean it up, like this:

var topSpeed : float = 160;
var numberOfGears : int = 5;
private var gearSpeeds : float[];

function SetupGears () {
    gearSpeeds = new float[numberOfGears];
    for (var i = 1; i < numberOfGears; ++i) {
        gearSpeeds[i] = topSpeed / (numberOfGears - 1) * i;
        Debug.Log("gearSpeeds[" + i + "]: " + gearSpeeds[i]);
    }
}

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images