all groups > flash actionscript > january 2005 >
You're in the

flash actionscript

group:

help cleaning up repetitive code


help cleaning up repetitive code posterboy
1/24/2005 8:34:50 PM
flash actionscript:
how can i take a bunch of repetitive code and clean it up into one nice neat
code? for instance...

this.createEmptyMovieClip ("tps_text", 100);
tps_text._x = 36;
tps_text._y = 7;

this.createEmptyMovieClip ("imaging_text", 200);
imaging_text._x = 36;
imaging_text._y = 66;

this.createEmptyMovieClip ("osd_text", 300);
osd_text._x = 36;
osd_text._y = 85;

this.createEmptyMovieClip ("bolts_text", 400);
bolts_text._x = 36;
bolts_text._y = 115;

this.createEmptyMovieClip ("closeouts_text", 500);
closeouts_text._x = 36;
closeouts_text._y = 101;

this.createEmptyMovieClip ("debris1_text", 600);
debris1_text._x = 36;
debris1_text._y = 92;

//ad nauseum
Re: help cleaning up repetitive code Jon Moyles
1/25/2005 9:18:03 AM
use arrays and a loop
say:

tps_x = new Array(value1,value2,...ext);
tps_y = new Array(value1,value2,...ext);
clipName = new Array(value1,value2,...ext);

for(i=0; i<clipName.length; i++){
clip = eval(clipName[i];
clip._x = tps_x[i];
clip._y = tps_y[i];
}

good luck
Re: help cleaning up repetitive code Byron Canfield
1/30/2005 9:03:26 PM
[quoted text, click to view]

Only that for loop will fail due to syntactic omission (left out a closing
paren), and would be better still done with the array access operator for
the reference resolution, as well (variable changed from "i" to "j" to
prevent italicization in the forum):

for(j=0; j<clipName.length; j++){
clip = this[clipName[j]];
clip._x = tps_x[j];
clip._y = tps_y[j];
}

And even better would be to store the clip names in the array as references,
so that forcing strings to resolve to references would be entirely
unnecessary:

clipName = [tps_text, imaging_text, osd_text, bolts_text, closeouts_text,
debris1_text];

Then the loop would be simply:

for(j=0; j<clipName.length; j++){
clipName[j]._x = tps_x[j];
clipName[j]._y = tps_y[j];
}

--
--------
Reality will not be altered to comply with preconceived notions.

Byron "Barn" Canfield

AddThis Social Bookmark Button