yeah I think you’re on track there - theres a lot going on in that line though. Ill break it down
i += (dir = (i == objNum-1) ? -Math.abs(dir) : (!i) ? Math.abs(dir) : dir);
first, what we want is i to go from 0 to objNum (the length of the array) back to 0 again and back and forth like that repetively.
-> <- -> <- -> …
so if the length was 5, we’d want numbers
0,1,2,3,4,3,2,1,0,1,2,3,4,3,2 …
logically, as you’ve gone through and figured out, youd have 2 types of ‘progression’ for this i variable. going up, and going down.
You initially differentiated this with the flag varibale and a ++ or – operator. What Im using is a dir (for direction) variable that is 1 for up and -1 for down. Because of this, it also doubles as the ++ and --. This means to have i go up and down, you just add dir to it. When dir is 1, i goes up, when dir is -1, i goes down.
What we need then is a way to determine when to change dir. When does that occur? When i is 0 and when i is objNum-1, since those are the extents of the array - the point at which i is at the end and the direction of its movement needs to be switched. when at 0, dir needs to be 1 to make i go back up, and at objNum-1, it needs to be -1 for going down. simple enough.
so what we have is:
- add dir to i
- check if i is 0, if so make dir 1
3)check if i is objNum-1, if so make dir -1
- if i is not 0 or objNum-1, dir stays the same.
1) **i += (dir** = (i == objNum-1) ? -Math.abs(dir) : (!i) ? Math.abs(dir) : dir);
2) i += (**dir = **(i == objNum-1) ? -Math.abs(dir) : **(!i) ? Math.abs(dir)** : dir);
3) i += (**dir = (i == objNum-1) ? -Math.abs(dir)** : (!i) ? Math.abs(dir) : dir);
4) i += (**dir = **(i == objNum-1) ? -Math.abs(dir) : **(!i)** ? Math.abs(dir) **: dir**);
in the (expression) ? statement1 : statement2, statement 1 is run when the expression is true, otherwise, statement 2 is run. its just a shorthand if (?) else (:).
So what this does is every frame assign dir to be either -dir, +dir or current dir, Math abs is used to force it to be positive (since you cant be sure if its positive or negative at the point of change… well its a good idea to force it like so at least). Then add that value on to i making it increement or decrement in the correct direction.
Going back to the ?:, let me just run through that real quick
(i == objNum-1) ? -Math.abs(dir) : (!i) ? Math.abs(dir) : dir);
theres two parts here - two conditions. The main (i == objNum-1) and the secondary (!i) which is ‘not i’ or the same as ‘if i == 0’. The main condition if true, returns -Math.abs(dir) which dir is assigned to. When that happen, all the following lines are skipped because the condition block ends. If main is false and (i == objNum-1) isnt true, then the secondary condition is run. That one checks to see if i is 0, if so, returning Math.abs(dir), otherwise returning just dir, which is the current value of dir which means dir doesnt change. so in if syntax that would be
// dir =
if (i == objNum-1){
return -Math.abs(dir); // negative direction
}else if (i == 0){
return Math.abs(dir); // positive direction
}else{
return dir; // no change
}