Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionScript: Duplicate objects

Status
Not open for further replies.

knuckle05

Programmer
Dec 17, 2001
247
CA
Hi All,

I'm in the process of creating a timeline (seek bar) for my media player on my website.

The Timeline itself is being created with Flash.

I have created two other objects to act as Markers on my timeline

BeginMark
EndMark

They are both defined as Movie Clip objects

Now, at run time I would like to be able to add as many Begin and End Markers as possible. The number could theoretically be infinite.

By default, I have placed one of each marker on my timeline by taking the objects from the Library window. I have created some ActionScript code for each object to behave a certain way.

My question is:

How can I dynamically add other "instances" of these Marker objects at runtime, and make them have unique names and still keep all of the same properties and ActionScript code and events that I've created for my initial 2 objects.

Can I just copy and paste them? What is the procedure to follow in ActionScript? Any direction would be much appreciated. Thanks

mike
 
Use duplicateMovieClip(), it takes three parameters: the original instance of the clip, the instance name you want to give to the duplicate and the stacking depth you want it set at on the stage (each instance must have a unique depth).

If you need to set multiple instances you can put this in a loop:

for(var i=1;i<=10;i++){
duplicateMovieClip(myClip,'myDuplicate'+i,i);
//the duplicate clip is now this['myDuplicate'+i] so you can control it like this:
this['myDuplicate'+i]._x=i*30;//etc

}
 
Thanks!

This is exactly what I was looking for. I''l try it out
 
How would I remove each instance?

Is there a delete method?
 
there's removeMovieClip() which can either contain a target in the parentheses to remove or be used as a method of the clip you want to remove: thisClip.removeMovieClip()
 
wangbar,

I'm trying to accomplish all of this through JavaScript.

I son't seem to be setting up my code properly though. Here is what I'm trying to do.

I have 2movie clips that I want to make duplicates of

SegStart
SegEnd

document.movie.duplicateMovieClip (document.movie._level0.SegStart, &quot;SegStart1&quot;, 1);

document.movie.duplicateMovieClip (document.movie._level0.SegEnd, &quot;SegEnd1&quot;, 1);

I keep getting document.movie._level0.SegStart is NULL or not an object.

If I remove the document.movie, I get an error stating that _level0 is not defined.

Any ideas?

 
I think you're going to have to rethink your approach here - the Flash movie can't be written and controlled at runtime like you're attempting. All the assets have to be in place in a published movie before you can control it via javascript methods, creating new objects like this won't work.

There is some good stuff on the macromedia site about flash/js interaction that might give you some clues.
 
hi,

Well, basically all I want to do is create dynamic instances of my movie clips at run time. This should be possible. I am now trying to do it from within Flash instead of javascript.

I've been trying for hours now, and still can't get it to work. PLease take a look.

onClipEvent (enterFrame) {
if (_level0.txtBlnCreateStartInstance == &quot;1&quot;) {
this._x = 100;
// increment instance counter
_level0.txtNumOfSegStartInstances = _level0.txtNumOfSegStartInstances+1;

duplicateMovieClip (this, &quot;SegStart&quot;+_level0.txtNumOfSegStartInstances, _level0.txtNumOfSegStartInstances+100);

this[&quot;SegStart&quot;+_level0.txtNumOfSegStartInstances]._x = int(_level0.txtCurPos);
}
_level0.txtBlnCreateStartInstance = false;
}


In one of the first lines I have this._x = 100, just to test to see if something is actually happening and it works. So I know that the routine is being entered.

The problem is that absolutely nothing happens at all. I do not see any clips being duplicated and no errors, though I'm new to Flash and not sure if you see errors at run-time. Any ideas?
 
This is a pretty dangerous way to duplicate clips because you're creating a new clip every frame which in turn are self-duplicating every frame, some clips will be overwriting each other too because only one instance can exist at a particular depth.

Try adding this as a test into your movie and maybe it'll help you find the solution to your problem. It duplicates a an offstage 'seedClip' every frame, drawing a diagonal line.

onClipEvent (enterFrame) {
++i;
_parent.seedClip.duplicateMovieClip('newClip'+i, i);
_parent['newClip'+i]._x =_parent['newClip'+i]._y= i;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top