PDA

View Full Version : is there something more efficient than conditionals?


shortly
07-15-2005, 01:03 PM
hi...

i've go the following code in a script i'm making. and i know there must be a more efficient way of doing the same thing. i just can't think of how.

this code is being called as part of a listener which is tracking a bunch of jpgs that are loading into MCs. on certain MCs i want to add some text dynamically and have that text repeat over 5 MCs. after each group of 5 i increase a counter that chooses a new variable to load into the dynamic text (that's the 'l' counter in the code below).

any thoughts?

thanks,

short.

oh, the 'k' was a counter for a loop to dynamically add MCs for the jpgs to load into... in case that helps!

if (k>=2 && k<=6){
initTxt();
if(k==6){
l++;
}
}
if (k>=7 && k<=11){
initTxt();
if(k==11){
l++;
}
}
if (k>=12 && k<=16){
initTxt();
if(k==16){
l++;
}
}
if (k>=16 && k<=21){
initTxt();
if(k==21){
l++;
}
}
if (k>=21 && k<=26){
initTxt();
if(k==26){
l++;
}
}
if (k>=26 && k<=31){
initTxt();
if(k==31){
l++;
}
}
if (k>=31 && k<=36){
initTxt();
if(k==36){
l++;
}
}
if (k>=36 && k<=41){
initTxt();
if(k==41){
l++;
}
}
if (k>=41 && k<=46){
initTxt();
if(k==46){
l++;
}
}
if (k>=46 && k<=51){
initTxt();
if(k==51){
l++;
}
}

CyanBlue
07-15-2005, 01:21 PM
How about this???

for (var k = 0; k < 100; k++)
{
if ((k > 1) && (k < 52))
{
initTxt();
i = Math.ceil(k / 5) - 1;
trace(k + " : " + i);
}
}

tg
07-15-2005, 01:50 PM
stick a break; in there, so if k==2, it doesnt need to loop another 98 times.

over all, conditionals are better in this situation (imo), cause six months to a year from now, a conditional statement will be easier to debug then aloop... at least it would be for me, since i don't generally use loops just to check a condition.... another option, would be to try switch statement.... but i'm not sure you can use a range in actionscript switchs.

shortly
07-16-2005, 08:16 AM
thanks for the quick replies CyanBlue and tg.

i'm not so sure i can implement that loop.

at the moment i'm using an onLoadComplete listener to check when the jpgs have finished loading before adding text using the initTxt() function (which uses createTextField to put text on that specific MC) - and the k value is already part of another loop (which uses createEmptyMovieClip in another function). so when each MC finishes loading the JPG i am assessing if it's in a range (2-6, 7-11, etc ) (just realised i was overlapping in my original code!) and then if it is in the first range add text for a particular variable to all those MCs, is it's in the second range add a different text to those MCs, etc.

the conditionals i'm using are working fine - i just feel like i'm sometimes limited in the way i program when using conditionals.

i'll have a look at switch as well (only used this for a menu before).

thanks,

short.

btw, it was v. frustrating that createTextField doesn't return a reference to the instance it creates - that was a good couple of hours wasted!

piratefish
08-05-2005, 04:53 AM
....another option, would be to try switch statement.... but i'm not sure you can use a range in actionscript switchs.

That's what I was going to suggest and if they could put it into function form it would help, but when doing that it would come up as true or false and wouldn't work at all.