# How do I apply same code to many movie clips

**URL:** <https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604>\
**Category:** Uncategorized\
**Created:** [May 28, 2004, 7:20pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604 "2004-05-28T19:20:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![smd\_ksu](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@smd\_ksu](https://forum.kirupa.com/u/smd_ksu)\
**Post date:** [May 28, 2004, 7:20pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/1 "2004-05-28T19:20:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 8:02pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/2 "2004-05-28T20:02:47Z")

</div>

Something like this?

```php

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…

```php

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 8:17pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/3 "2004-05-28T20:17:10Z")

</div>

That worked perfectly!

Thank you very much

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 8:18pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/4 "2004-05-28T20:18:39Z")

</div>

np 🙂 show me your results…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 9:04pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/5 "2004-05-28T21:04:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 9:06pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/6 "2004-05-28T21:06:53Z")

</div>

error?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2004, 10:03pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/7 "2004-05-28T22:03:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 29, 2004, 3:05pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/8 "2004-05-29T15:05:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 29, 2004, 4:45pm UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/9 "2004-05-29T16:45:32Z")

</div>

```php

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();
};

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 30, 2004, 6:12am UTC](https://forum.kirupa.com/t/how-do-i-apply-same-code-to-many-movie-clips/111604/10 "2004-05-30T06:12:34Z")

</div>

thanks RvGate!
