# Duplicating/removing movie clips

**URL:** <https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272>\
**Category:** flash\
**Created:** [March 25, 2006, 10:07am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272 "2006-03-25T10:07:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [March 25, 2006, 10:07am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/1 "2006-03-25T10:07:14Z")

</div>

[www.geocities.com/nitihasa/duplicate.fla](http://www.geocities.com/nitihasa/duplicate.fla)

I tried to make it so:[LIST=1]  
[_]Every second a new ball (“Q”) will be duplicated.  
[_]When a ball touches one of the two walls, it comes back.  
[\*]When a ball gets out of the boundary, the movie clip is removed.[/LIST]However, it works only with the first ball. When the third ball is out, the second ball disappears in the same time (before leaving the boundary), and thus with the following balls.  
Can anyone solve it?

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 25, 2006, 12:05pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/2 "2006-03-25T12:05:05Z")

</div>

i cant open your fla - is it MX 2004?

but anyway - use a counter to keep track of your duplicate mcs ie every time you make the duplicate use, and store a number in each duplicate mc

```auto

duplicateMovieClip(name_of_mc, "duplicate_"+counterVar, counterVar);
_root["duplicate_"+counterVar].number = counterVar;
//then right at the end of whatever happens every time you duplicate
counterVar++;

```

put an onEnterFrame inside the actual mc that gets duplicated to test the x and y positions of it, and if its off-stage use removeMovieClip(_root["duplicate_"+this.number]); to get rid of the ones that meet the x/y test

---

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [March 25, 2006, 6:37pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/3 "2006-03-25T18:37:29Z")

</div>

First of all, thanks.  
However, now it just removes the ball as soon as it is duplicated.  
My Flash is indeed MX 2004 (and for some reason neither can I open other MX 2004 .fla’s that I download).

Anyway, I’ll just paste the actionscript I put in the ball movie-clip:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#0000FF]onClipEvent[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
i = [COLOR=#000080]1[/COLOR];  
t = [COLOR=#000080]0[/COLOR];  
v = [COLOR=#000080]4[/COLOR];  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]\_y[/COLOR] = [COLOR=#000080]0[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]onClipEvent[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//trace(“Q”+i);_[/COLOR]  
t = t+[COLOR=#000080]1[/COLOR]/[COLOR=#000080]100[/COLOR];  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]\_y[/COLOR] = [COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]\_y[/COLOR]+v;  
[COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
v = -[COLOR=#000080]4[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
v = -[COLOR=#000080]4[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]duplicateMovieClip[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]“Q”[/COLOR]+i, [COLOR=#0000FF]getNextHighestDepth[/COLOR]COLOR=#000000[/COLOR], [COLOR=#000000]{[/COLOR][COLOR=#0000FF]\_x[/COLOR]:[COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]random[/COLOR]COLOR=#000000[/COLOR]\*[COLOR=#000080]550[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000])[/COLOR];  
[COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“Q”[/COLOR]+i[COLOR=#000000]][/COLOR].[COLOR=#0000FF]number[/COLOR] = i;  
t = [COLOR=#000080]0[/COLOR];  
i += [COLOR=#000080]1[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“Q”[/COLOR]+i[COLOR=#000000]][/COLOR].\_y\>=[COLOR=#000080]400[/COLOR] or [COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“Q”[/COLOR]+i[COLOR=#000000]][/COLOR].\_y\<=[COLOR=#000080]0[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“Q”[/COLOR]+[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]number[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]removeMovieClip[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[/LEFT]  
[/FONT]

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 25, 2006, 10:02pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/4 "2006-03-25T22:02:09Z")

</div>

first of all wrap your as stuff in

```auto

```

tags - it makes oit much easier to read, and when u paste it from flash, use the auto format first…

```auto

onClipEvent (load) {
	i = 1;
	t = 0;
	v = 4;
	this._y = 0;
}
onClipEvent (enterFrame) {
	//trace("Q"+i);
	t = t+1/100;
	this._y = this._y+v;
	if (this.hitTest(_root.wall1)) {
		v = -4;
	}
	if (this.hitTest(_root.wall2)) {
		v = -4;
	}
	if (t>=1) {
		this.duplicateMovieClip("Q"+i, getNextHighestDepth(), {_x:Math.random()*550});
		_root["Q"+i].number = i;
		t = 0;
		i += 1;
	}
	if (_root["Q"+i]._y>=400 or _root["Q"+i]._y<=0) {
		_root["Q"+this.number].removeMovieClip();
	}
}

```

anyway that code seems very wrong - could you save it as an MX so i can see if i can open that?

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 25, 2006, 10:04pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/5 "2006-03-25T22:04:37Z")

</div>

woops - [as][/as] w/out the spaces will format your code to look like as…

---

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [March 27, 2006, 11:31am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/6 "2006-03-27T11:31:42Z")

</div>

> **[Yahoo Small Business - Cheap Domains, Web Hosting, Website Builder, Ecommerce...](https://smallbusiness.yahoo.com/)**
>
> Yahoo Small Business Offers Cheap Domain Names, Web Hosting, Easy Website Builder, Business Email, Local Listings, and Ecommerce Solutions for Your Small Business.

  
Hopefully that works.

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 27, 2006, 11:23pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/7 "2006-03-27T23:23:07Z")

</div>

ok ill check it out

edit: nope - unexpected file format still…

are you using flash MX 2004 version 7.2? if not update it…

---

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [April 7, 2006, 8:22pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/8 "2006-04-07T20:22:27Z")

</div>

Should work now:  
[http://www.geocities.com/nitihasa/duplicate.fla](http://www.geocities.com/nitihasa/duplicate.fla)  
[http://www.geocities.com/nitihasa/duplicate.swf](http://www.geocities.com/nitihasa/duplicate.swf)

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 9, 2006, 7:27am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/9 "2006-04-09T07:27:09Z")

</div>

ok ill check it out 🙂

---

<div class="post-metadata">

**Author:** ![scotty](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@scotty](https://forum.kirupa.com/u/scotty)\
**Post date:** [April 9, 2006, 8:30am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/10 "2006-04-09T08:30:51Z")

</div>

Skip the code on your electron mc and put this in the first frame

```auto
stop();
i = 0;
t = 0;
function dup() {
	t++;
	if (!(t%30)) {
		i++;
		var electron = electron.duplicateMovieClip("electron"+i, this.getNextHighestDepth(), {_x:Math.random()*550});
		moveIt(electron);
	}
}
function moveIt(clip) {
	clip._y = 0;
	var v = 4;
	clip.onEnterFrame = function() {
		this._y += v;
		if (this.hitTest(_root.wall1) || this.hitTest(_root.wall2)) {
			v = -4;
		}
		if (this._y>=400 || this._y<=0) {
			this.removeMovieClip();
		}
	};
}
moveIt(electron);
setInterval(this, "dup", 30);

```

scotty(-:

---

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [April 9, 2006, 1:39pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/11 "2006-04-09T13:39:54Z")

</div>

Thank you both. This code works fine.  
But, can you please explain me what you did that now it works (mostly the dup() function and the setInterval(this, “dup”, 30).  
Also, how can I modify it so t measures the seconds (when I change t++ into t=t+1/100 and modify the dup() it doesn’ work)? It is important for the Physics formulas that will follow.

---

<div class="post-metadata">

**Author:** ![scotty](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@scotty](https://forum.kirupa.com/u/scotty)\
**Post date:** [April 9, 2006, 3:02pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/12 "2006-04-09T15:02:03Z")

</div>

You have the fps at 100 so call the dup function every 10 msecs

```auto
setInterval(this, "dup", 10);

```

and change the if statement, so it duplicates every second

```auto
function dup() {
    t++;
    //returns false every second
    if (!(t%100)) {
        i++;
        var electron = electron.duplicateMovieClip("electron"+i, this.getNextHighestDepth(), {_x:Math.random()*550});
        moveIt(electron);
    }
}

```

scotty(-:

---

<div class="post-metadata">

**Author:** ![Carb0n1zer](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@Carb0n1zer](https://forum.kirupa.com/u/Carb0n1zer)\
**Post date:** [April 9, 2006, 9:42pm UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/13 "2006-04-09T21:42:10Z")

</div>

Thanks.  
Another question:  
If I want to use the v variable (which was set in the frame’s actions) from within the movie-clip (such as using onClipEvent(enterFrame) {trace(v)}, how do I call it? \_root.v/\_root.parent.v do not work.

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 10, 2006, 6:29am UTC](https://forum.kirupa.com/t/duplicating-removing-movie-clips/183272/14 "2006-04-10T06:29:41Z")

</div>

heres a fla for you

if you want to access v from inside the elctron u just use trace(v); cause i put all that stuff in the actual mc.

if you want to access it from the \_root timeline - use this[“electron”+n].v

n is a number. to access the last one created - use this[“electron”+numberOfCircles].v
