A ridiculous AS problem for you

hello everybody,

my ridiculous problem :

this an AS code which work :

ecart_cat = 90;
destx_cat1 = 150.1;
destx_cat2 = destx_cat1+ecart_cat;
destx_cat3 = destx_cat1+2ecart_cat;
destx_cat4 = destx_cat1+3
ecart_cat;
destx_cat5 = destx_cat1+4*ecart_cat;

cat1.onEnterFrame = function() {
distpx_cat1 = destx_cat1-cat1._x;
cat1._x += distpx_cat1*(0.1);
if (Math.abs(distpx)<1) {
delete cat1.onEnterFrame;
}
};
cat2.onEnterFrame = function() {
distpx_cat2 = destx_cat2-cat2._x;
cat2._x += distpx_cat2*(0.1);
if (Math.abs(distpx)<1) {
delete cat2.onEnterFrame;
}
};
cat3.onEnterFrame = function() {
distpx_cat3 = destx_cat3-cat3._x;
cat3._x += distpx_cat3*(0.1);
if (Math.abs(distpx)<1) {
delete cat3.onEnterFrame;
}
};
cat4.onEnterFrame = function() {
distpx_cat4 = destx_cat4-cat4._x;
cat4._x += distpx_cat4*(0.1);
if (Math.abs(distpx)<1) {
delete cat4.onEnterFrame;
}
};
cat5.onEnterFrame = function() {
distpx_cat5 = destx_cat5-cat5._x;
cat5._x += distpx_cat5*(0.1);
if (Math.abs(distpx)<1) {
delete cat5.onEnterFrame;
}
};

I just want to reduce it like this :

n = 6;
for (i=1; i<n; i++) {
[“cat”+i].onEnterFrame = function() {
distpx_[“cat”+i] = destx_[“cat”+i]-[“cat”+i]._x;
[“cat”+i].x += distpx[“cat”+i]*(0.1);
if (Math.abs(distpx)<1) {
delete [“cat”+i].onEnterFrame;
}
};
}

but it doesn’t work :frowning:
please, help me.

you need to add the path for dynamic referencing like that, her put this and it should work assuming cat1-n are on the root level
n = 6;
for (i=1; i<n; i++) {
root[“cat”+i].onEnterFrame = function() {
distpx
[“cat”+i] = destx_[“cat”+i]-_root[“cat”+i]._x;
_root[“cat”+i].x += distpx[“cat”+i]*(0.1);
if (Math.abs(distpx)<1) {
delete _root[“cat”+i].onEnterFrame;
}
};
}


ecart_cat = 90;
cat1.destx = 150.1;
cat2.destx = cat1.destx+ecart_cat;
cat3.destx = cat1.destx+2*ecart_cat;
cat4.destx = cat1.destx+3*ecart_cat;
cat5.destx = cat1.destx+4*ecart_cat;

for(var a=1;i<=5;a++){
	this["cat"+a].onEnterFrame = function(){
		var distpx = this.destx-this._x;
		this._x += distpx*0.1;
		if(Math.abs(distpx)<1) delete this.onEnterFrame;
	}
}

sorry, but paradox’s code don’t work :
when I export the animation, nothing happens.

and Voetsjoeba’s one make Flash MX2004 closed, with an error message.
it’s very strange… i don’t understand…

please still help me !!! :puzzle:

Oh lol ! I made a typo, that i should have been an a:


ecart_cat = 90;
cat1.destx = 150.1;
cat2.destx = cat1.destx+ecart_cat;
cat3.destx = cat1.destx+2*ecart_cat;
cat4.destx = cat1.destx+3*ecart_cat;
cat5.destx = cat1.destx+4*ecart_cat;

for(var a=1;a<=5;a++){
	this["cat"+a].onEnterFrame = function(){
		var distpx = this.destx-this._x;
		this._x += distpx*0.1;
		if(Math.abs(distpx)<1) delete this.onEnterFrame;
	}
}

the “i” instead of the “a”… héhé… :slight_smile:

yeees ! :beer: thanx a lot !! it works now !!!

You’re welcome :slight_smile: