# Adding actions to duplicated MC's?

**URL:** <https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174>\
**Category:** flash\
**Created:** [October 29, 2003, 6:57am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174 "2003-10-29T06:57:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sef](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@sef](https://forum.kirupa.com/u/sef)\
**Post date:** [October 29, 2003, 6:57am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/1 "2003-10-29T06:57:55Z")

</div>

Im trying to duplicate a movieclip, that is trailing to my mouse (only on the \_x axis). Now I want to duplicate that movieclip several times and making the duplicates follow the first movieclip, but with a \_x difference between them (lineair steps).

Now I can get the first movieclip to follow the mouse on the \_x axis, and I can get to duplicate it several times, but I get stuck when trying to add the actions to the ones I’ve duplicated…

Any ideas? :-\

---

<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:** [October 29, 2003, 7:32am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/2 "2003-10-29T07:32:45Z")

</div>

What code do you have currently ?

---

<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:** [October 29, 2003, 8:31am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/3 "2003-10-29T08:31:44Z")

</div>

This is the function for following the mouse over the \_x axis, it’s currently stuck on a button…

```auto
	
on(release){
   carrierMC.onEnterFrame = function (){
	 with(this){
		distX = (_root._xmouse - _x)
		_x += distX*0.2;
		 }
 	}
}

```

This is the code for duplicating the main MC.

```auto

amount = 4;
	while (amount>0) {
		duplicateMovieClip (_root.carrierMC, "mc"+i, i);
		setProperty ("mc"+i, _x, random(500));
		i++;
		amount--;
	}

```

I added this next piece right under that one.

```auto

["mc"+i]._visible = false

```

just to try and test that, but it didn’t work

well this code is pretty messy, because I was just playing with it a little bit.  
Im eventually not going to use it as a mouse trailer but more like a component for displaying pictures, this is why I want to use duplicated MC’s (and external jpeg’s). Now I don’t know how to load those correctly into a duplicated movieclip but I reckon it should be similar to the code for making the duplicated mc’s work.

Ok, dat laatste gedeelte maakt het allemaal een beetje heel erg verwarrend… sorry…

---

<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:** [October 29, 2003, 1:00pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/4 "2003-10-29T13:00:54Z")

</div>

> 

Ok, dat laatste gedeelte maakt het allemaal een beetje heel erg verwarrend… sorry…

Inderdaad 😛

I’m not sure what exactly it is you are trying to achieve. To load to a duplicated movieclip or to create a mouse trailer using duplicated movieclips ?

Anyway, a handier code for

```auto

amount = 4;
	while (amount>0) {
		duplicateMovieClip (_root.carrierMC, "mc"+i, i);
		setProperty ("mc"+i, _x, random(500));
		i++;
		amount--;
	}

```

is

```auto

amount=4;
for(i=0;i<=amount;i++){
m = _root.carrierMC.duplicateMovieClip ("mc"+i, i);
m._x = Math.floor(Math.random()*500+1);
}

```

Using m as reference to the just duplicated movieclip, you can write code much simpler. Just use m to target the las duplicated movieclip 🙂

Using the same way, you can add dynamic event handlers to m:

```auto

amount=4;
for(i=0;i<=amount;i++){
m = _root.carrierMC.duplicateMovieClip ("mc"+i, i);
m._x = Math.floor(Math.random()*500+1);
m.onEnterFrame = function(){
//do stuff here
}
}

```

---

<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:** [October 30, 2003, 10:38am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/5 "2003-10-30T10:38:34Z")

</div>

yeah this was the kind of code I was looking for, much better than that old FLash 4 duplicateMC stuff I reckon!  
So I’ve played around with it a little, but when I tried to add some movement to the clips I ran into a problem because the function() command is only running once;  
this is my code now (thanks to Voetsjoeba !):

```auto

amount=4;
for(i=1;i<=amount;i++){
        m = _root.carrierMC.duplicateMovieClip ("mc"+i, i);
        m._x = i*125-125;
        m.loadMovie(i+".jpg")
		m.onEnterFrame = function (){
 				m.distX = (_root._xmouse - m._x)
				m._x += m.distX*0.2;
 	}      
}

```

---

<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:** [October 30, 2003, 12:04pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/6 "2003-10-30T12:04:16Z")

</div>

I’ don’t know what your problme is exactly. Can you please explain further ?

---

<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:** [October 30, 2003, 3:08pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/7 "2003-10-30T15:08:35Z")

</div>

well this code only runs for as long as i is being iterated. and only when that happens.  
So when you run a trace on m.distX ( trace(m.distX) ) you will get only 4 values, and the movieclips change accordingly, but that is not very usefull, since they move only once.

So what you want them to do is keep following the mouse, after the for([…]) loop has ended, but that doesn’t happen with this code, I don’t really understand why though.

Hope that explains it a little bit…

PS: is Ghent niet zonder G of zie ik iets over het hoofd? (nou ja ik ben natuurlijk een nederlander en geen belg 🙂 )

---

<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:** [October 30, 2003, 4:55pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/8 "2003-10-30T16:55:27Z")

</div>

Gent in het engles is Ghent 😉

This should do

```auto

amount=4;
for(i=1;i<=amount;i++){
        m = _root.carrierMC.duplicateMovieClip ("mc"+i, i);
        m._x = i*125-125;
        m.loadMovie(i+".jpg")
		m.onEnterFrame = function (){
 				this.distX = (_root._xmouse - this._x)
				this._x += this.distX*0.2;
 	}      
}

```

Should work.

---

<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:** [October 31, 2003, 3:09pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/9 "2003-10-31T15:09:41Z")

</div>

sadly, that code doesn’t seem to work either. Wait I’ll through up the .fla (just a sec)

---

<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:** [October 31, 2003, 3:12pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/10 "2003-10-31T15:12:03Z")

</div>

That sure did work for me :-\ but ok 🙂

---

<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:** [October 31, 2003, 3:13pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/11 "2003-10-31T15:13:46Z")

</div>

– attachement –

the \*.fla contains the code of two posts earlier, e.g. not the edited version by Voetsjoeba (although there isnt a lot of difference between them).

I also included the externally loaded files to keep the file working.

pictures courtesy of www.orandajin.nl 🙂

---

<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:** [October 31, 2003, 3:29pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/12 "2003-10-31T15:29:16Z")

</div>

Wow, this behaviour is weird … lemme look into it.

---

<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:** [October 31, 2003, 3:37pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/13 "2003-10-31T15:37:57Z")

</div>

Solved this the easy way.

\*Oh, you can delete the empty movieclip that is on stage 🙂

---

<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:** [November 1, 2003, 5:43am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/14 "2003-11-01T05:43:59Z")

</div>

yup that code works great, cept there is one problem; everything on the stage moves along in the mouse trailer function, so not just the created movieclips…

this looks really strange offcourse, and I didnt figure out yet how to stop it from happening.

Any ideas?

(sorry for my lameness :\*( )

---

<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:** [November 1, 2003, 8:16am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/15 "2003-11-01T08:16:33Z")

</div>

Woops ! 😛 Lemme fix that.

---

<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:** [November 1, 2003, 8:25am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/16 "2003-11-01T08:25:43Z")

</div>

Here:

---

<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:** [November 1, 2003, 2:11pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/17 "2003-11-01T14:11:07Z")

</div>

yup you did it :love:

only one more question (I hope): how can I trigger the movement part with a button? As in; I only want the last part of the code to be executed after I’ve pressed a button elsewhere on the stage?

With the last code I got it to work but I can’t get it working in the new version anymore :puzzle:

anyway, i’d like to thank you very much for you help already voetsjoeba, and Im sorry that Im a little confusing in what I want to do because Im learning how to do this first, so I can make a useful implementation of it later. Like: Im not going to use this exact code but if I can get this to work I know how to get something else to work, so I could make that myself. So thanks a lot as well for teaching me a whole lotta new stuff =)

---

<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:** [November 1, 2003, 4:10pm UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/18 "2003-11-01T16:10:43Z")

</div>

Here you are 😉

---

<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:** [November 9, 2003, 4:38am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/19 "2003-11-09T04:38:33Z")

</div>

thanks, this is helping me a lot 🙂

but I am afraid I have to ask one more question… sowwy :\*(

What If I wanted to add a mask over the loaded images, how would I go about and do that?

---

<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:** [November 9, 2003, 9:28am UTC](https://forum.kirupa.com/t/adding-actions-to-duplicated-mcs/34174/20 "2003-11-09T09:28:57Z")

</div>

Just create a movieclip with your mask in it, set the registration point to the left so you can easily control it’s position relative to the images, then use the setMask method 🙂
