Stepping up and down arrays

what i want to do is go up and down an array, getting vars 1 to n and then back down from n to 1 and then back up again. i wrote this lame code that doesn’t really work:


//vars to use in if statement
i=1;
objNum=_root.hexArray.length;
flag="up";

this.onEnterFrame=function(){
		if(i<objNum and flag=="up"){
		i++;
		myColor.setRGB(_root.hexArray*);
		trace("1");
	}else if (i==objectNum){
	flag="down";
			i--;
	myColor.setRGB(_root.hexArray*);
	trace("3");
} else if(i<objNum and flag=="down"){
		i--;
	myColor.setRGB(_root.hexArray*);
	trace("2");
}else if (i==0){
	flag="up";
			i++;
		myColor.setRGB(_root.hexArray*);
		trace("4");
	}
}

i know there are better ways out there to do this–anybody care to enlighten me?
tia,
-mojo

you had both objNum and objectNum…


this.onEnterFrame = function() {
	if (i < objNum and flag == "up") {
		i++;
	} else if (i == objNum and flag == "up") {
		flag = "down";
		i--;
	} else if (i < objNum and flag == "down" && i != 0) {
		i--;
	} else if (i == 0 && flag == "down") {
		flag = "up";
		i++;
	}
	myColor.setRGB(_root.hexArray*);
};

now, all you have to do is stop it

nunomira! :beam:

watch out for lessthan signs followed by any character other than a space, the board will parse them out thinking its html code :wink:

as for the script, Im reminded of this thread
http://www.kirupaforum.com/showthread.php?s=&threadid=11427

there I have an example which oscilates (like everyone elses) with


gotoAndStop(_currentframe + (dir = (_currentframe == _totalframes) ? -Math.abs(dir) : (_currentframe == 1) ? Math.abs(dir) : dir));

or course in terms of the array it would be like



i = 0;
dir = 1;
objNum = _root.hexArray.length;
this.onEnterFrame = function(){
	i += (dir = (i == objNum-1) ? -Math.abs(dir) : (!i) ? Math.abs(dir) : dir);
	myColor.setRGB(_root.hexArray*);
}

[edit]Sorry, forgot to declare dir as 1 :wink: dir is the direction of movement in the array, 1 for moving forward, -1 for backwards. Not set or at 0 is not going through the array, so unless you knew that or were able to figure it out the above code wouldnt work :slight_smile: [/edit]

nuno, thanks for picking up my sloppy code(-:
this was also key: else if (i < objNum and flag == “down” && i != 0)
funny, i saw your original post and it was cut like senocular said, so i went back and cleaned up my code, where i saw my error, but didn’t fix it as well=)

senoc, thanks, that’s exactly what i was looking for
i knew there was a way to boil it down!:wink:
can you bear with me while i try to figure it out?

i += (dir = (i == objNum-1) ? -Math.abs(dir) : (!i) ? Math.abs(dir) : dir);

ok, it’s a 4 part statement with 2 conditionals, right?
if the first part is true, it returns the second part, otherwise it goes to the third part and if that’s true it returns the fourth part? wow.
i just looked it up the ?: symbol in the fd–am i on the right track?
gotta know how these things work–i luv to break it down…:beam:
-mojo

yeah I think you’re on track there - theres a lot going on in that line though. Ill break it down :slight_smile:

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:

  1. add dir to i
  2. check if i is 0, if so make dir 1
    3)check if i is objNum-1, if so make dir -1
  3. 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
}

:slight_smile:

FAN - TASTIC!!!
thank you for breaking it down, that is some great code,
very elegant, indeed:beam: especially the way you hook i and dir together–i’d never even seen ‘?:’ and ‘!¡’ before…(it took me a while to find ‘¡’ on my keyboard) :stuck_out_tongue:

powerful stuff indeed, i can’t wait to start boiling my if statements down using this method…
i’m giving you an ‘A+’ or gold star, or whatever’s the best rating…(-:
-mojo