Optimize my a += 1 so it counts over and over between 1 and 6

All variables start offset by 1 (a=1, b+2, c+3, etc…)

I want the values to all start over when they reach 7.
You can see the lower half of code displays these images, basically rotating around a circle.

What is most efficient way to script? I assume an array.

a += 1; if (a==7) {a=1};
b += 1; if (b==7) {b=1};
c += 1; if (c==7) {c=1};
d += 1; if (d==7) {d=1};
e += 1; if (e==7) {e=1};
f += 1; if (f==7) {f=1};
loadmovie(“menus/MenuImg” + a + “.jpg”,“MenuImg1”);
loadmovie(“menus/MenuImg” + b + “.jpg”,“MenuImg2”);
loadmovie(“menus/MenuImg” + c + “.jpg”,“MenuImg3”);
loadmovie(“menus/MenuImg” + d + “.jpg”,“MenuImg4”);
loadmovie(“menus/MenuImg” + e + “.jpg”,“MenuImg5”);
loadmovie(“menus/MenuImg” + f + “.jpg”,“MenuImg6”);

Please help? Thought it does work this way, I know I am far from proper/efficent in my scripting.

Ron

Are you using Flash 5 or MX?

If MX, try this…

_root.onLoad = function() {
	a = 1;
};
_root.onEnterFrame = function() {
	a += 1;
	if (a == 7) {
		a = 1;
	}
	trace(a);
};

Where all your starting variables go under onLoad and all the increment coding goes under onEnterFrame

<B>EDIT: </B>I added trace in there to show you that it works.

Thanks for quick reply. Ok, I didn’t think of declaring var in onLoad, I am doing it on _parent timeline (this is working). But I am going to move them to onLoad so they are visible there.

My question was more to do with optimizing the if = 7 part. I didn’t know if there was simpler notation to do the same thing for each variables increment and check.

Thanks for help,

Ron

I stick with the good ol’ way.

There is the notorious ?: way of doing an if else statement, but that can be tedious sometimes if you don’t know how to use it.

function doNums(value) {
for (var i = 1; i<7; i++) {
loadMovie(value+i+".swf", "_root.empty");
}
}

You would call the function by doNums(“a”); which would load a1.swf to a6.swf - change *.swf to whatever and _root.empty to whatever, including the target to the mc.

You could use the same function but just use doNums(“b”); which would load b1.swf to b6.swf. Same for doNums(“c”); Just call the function with the correct target path and loop it. It would keep going from 1 to 6.

In theory…

3dron -

there’s a marvelous operator, %, called a ‘modulo’ that will take the remainder of the result of dividing the former by the latter.

ok, that’s pretty confusing but it’s actually very simple and does exactly what you want. eg:

8%3 equals 2 because 8/3 leaves a remainder of 2.

make sense?

thus you can accomplish what you want by taking the modulo 6 + 1 of the incrementing counter:


loadmovie("menus/MenuImg" + ((a++)%6+1) + ".jpg","MenuImg1");

just a tip, get used to zero basing your lists, code just works better that way.

And sbeener comes and knocks everyone off the chart :slight_smile:

I always wondered how that % worked. Thanks for the info sbeener:)

I was about to come here and scream because I have written an article about the % operator, but I’ve checked and it’s not online yet :-\

pom :smirk:

Incredible. It works great and I understand it. (2 pluses)

I even figured out hot to implement my code this way, so I don’t have to trakc 6 variables:

_global.ffa++;
loadmovie(“menus/MenuImg” + ((_global.ffa+1)%6) + “.jpg”,“MenuImg1”);
loadmovie(“menus/MenuImg” + ((_global.ffa+2)%6) + “.jpg”,“MenuImg2”);
loadmovie(“menus/MenuImg” + ((_global.ffa+3)%6) + “.jpg”,“MenuImg3”);
loadmovie(“menus/MenuImg” + ((_global.ffa+4)%6) + “.jpg”,“MenuImg4”);
loadmovie(“menus/MenuImg” + ((_global.ffa+5)%6) + “.jpg”,“MenuImg5”);
loadmovie(“menus/MenuImg” + ((_global.ffa+6)%6) + “.jpg”,“MenuImg6”);
Thanks

Ron

Well, a loop would help:

for (var i=0;i < 6;i++){
    loadmovie("menus/MenuImg" + ((++_global.ffa)%6) + ".jpg","MenuImg1");
}

Or something like that… :sleep:

By the way, why do you declare a global variable:q: :cyclops:

3dron - sweet. way to go.

ily - nice try but it won’t work, can you spot your error?

Nope, cos it’s 7 am here, and I haven’t sleeped for too long… I knew something was wrong when I wrote it though :stuck_out_tongue: But please tell me!

Oh OK, the target changes. Sorry, as I said, I need some sleep.:sleep: :sleep:

pom :smirk:

; ) just pokin’ fun.

No, I appreciate your attempt to make me use by brain at such an avanced hour… :stuck_out_tongue:

I declare a global because in under each movieclip I have an “on(release)”

which goes to an swf named in ffarray.

It is called by loadmovie(ffarray(_global.ffa)) etc…
And I might use it in other places also for titling effects, sounds, etc…

Also my code repeats every 100 frames. Do I want to increment ++ on every loadmovie?

I guess it looks like it would work, because next time I come around to the first loadmovie again, I should end up with one number higer than I did last time right?

RR

is it legal to be THIS advanced…

… :: goes to kill himself as hopes of learning flash are buried under this thread ::

GAD! what i would give for a brain transplant with one of u
id love to be able to speak that fluently about AS
U RULE

Don’t worry, where just pretending we know what we’re talking about. Stay a few days, and you’ll be able to do that too… :stuck_out_tongue: