Duplicating/removing movie clips

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?

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


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

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]

first of all wrap your as stuff in


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


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?

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


Hopefully that works.

ok ill check it out

edit: nope - unexpected file format still…

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

Should work now:
http://www.geocities.com/nitihasa/duplicate.fla
http://www.geocities.com/nitihasa/duplicate.swf

ok ill check it out :slight_smile:

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

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(-:

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.

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

setInterval(this, "dup", 10);

and change the if statement, so it duplicates every second

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(-:

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.

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