How do I apply same code to many movie clips

I have alot of mc’s and I want them all to have the same code applied to them without having to copy/paste all the code in each one and also it would be easier for any changes latter. I have tried making the code a function and then calling it inside the mc’s but it did not work and I am fairly new at functions themselves so I am stuck.

This is the code below-

onClipEvent (load) {
targetx = this._x;
targety = this._y;
this._y = 0;
this._x = 0;
speed = 4;
size = random(100);
this._xscale = size+100;
this._yscale = size+100;
onEnterFrame = function () {
this._x += (targetx-this._x)/speed;
this._y += (targety-this._y)/speed;
this._alpha += (0-this._alpha)/7;
if (this._alpha<=0) {
this.removeMovieClip();
}
};
}

The code marks the mc position, then moves it to 0,0 then moves back to the original postion while degrading alpha. I set it up this way so I can arrange the mc in any shape and the mc’s will look like they explode into the shape when played. It is used for a fireworks effect.

Thanks

Something like this?


MovieClip.prototype.MCmove = function() {
	this.targetx = this._x;
	this.targety = this._y;
	this.speed = 4;
	this._y = 0;
	this._x = 0;
	this.size = random(100);
	this._xscale = this.size+100;
	this._yscale = this.size+100;
	this.onEnterFrame = function() {
		this._x += (this.targetx-this._x)/this.speed;
		this._y += (this.targety-this._y)/this.speed;
		this._alpha += (0-this._alpha)/7;
		if (this._alpha<=0) {
			this.removeMovieClip();
		}
	};
};
myMC1.MCmove();
myMC2.MCmove();

myMC1 and 2 are the movieclip instance names…

====
edit:

to apply this to many objects, use a loop…


MovieClip.prototype.MCmove = function() {
	// change to random position
	this._x = random(Stage.width);
	this._y = random(Stage.height);
	this.targetx = this._x;
	this.targety = this._y;
	this.speed = 4;
	// i centered it for you... change whatever you want...
	this._y = Stage.height/2;
	this._x = Stage.width/2;
	this.size = random(100);
	this._xscale = this.size+100;
	this._yscale = this.size+100;
	this.onEnterFrame = function() {
		this._x += (this.targetx-this._x)/this.speed;
		this._y += (this.targety-this._y)/this.speed;
		this._alpha += (0-this._alpha)/7;
		if (this._alpha<=0) {
			this.removeMovieClip();
		}
	};
};
// set total of particles
total = 50;
for (i=0; i<total; i++) {
	// add object to stage, from the library (check the linkage)
	newMC = _root.attachMovie("clip", "clip_"+i, i);
	// apply the code
	newMC.MCmove();
}

read the comments

That worked perfectly!

Thank you very much

Just saw your edit, that was going to be the next thing I was going to try.

np :slight_smile: show me your results…

Heres what I have so far, have not really tried to apply it yet.

error?

It worked for me at home and work but I re-zipped it anyway with the .swf and a mx version. Right now you click for the firework to go off and then it replays on further clicks. I will eventually have it duplicate and fire where you click but just don’t have the time right now.

problem:
is there a similar way of compacting the script even more using an array or any other method.

situation:
i have plenty of mcs sharing the same property
eg.

with (bio) {
_alpha = 0;
_visible = false;
}
with (contxt) {
_alpha = 0;
_visible = false;
}
with (contact) {
_alpha = 0;
_visible = false;
}

etc.

if i would to use the suggested solution above:

MovieClip.prototype.MChide = function() {
this._alpha = 0;
this._visible = false;
}

nav.MChide();
bio.MChide();
contxt.MChide();
contact.MChide();
etc.

question:
everytime there’s a new mc i have to add another .MChide(); to it, so, is there a way i can have something like this:

var MC2hide = new Array(nav,bio,contxt,contact…etc)

with (MC2hide){
this._alpha = 0;
this._visible = false;
}

sorry, i’m very very new to actionscript…hope you guys can show me some light on this to me. thanks so much.


MovieClip.prototype.Move = function(newx, newy, speed) {
	this.onEnterFrame = function() {
		this._x += (newx-this._x)/speed;
		this._y += (newy-this._y)/speed;
	};
};
function moveAll() {
	myObjects = new Array("mc1", "mc2", "mc3", "mc4", "mc5");
	for (i=0; i<=myObjects.length; i++) {
	      _root[myObjects*].Move(_root._xmouse, _root._ymouse, i+1);
	}
}
_root.onEnterFrame = function() {
	moveAll();
};

thanks RvGate!