View comments | RSS feed

MovieClip.attachMovie()

Availability

Flash Player 5.

Usage

my_mc.attachMovie(idName:String, newName:String, depth:Number [, initObject:Object]) : MovieClip

Parameters

idName The linkage name of the movie clip symbol in the library to attach to a movie clip on the Stage. This is the name entered in the Identifier field in the Linkage Properties dialog box.

newname A unique instance name for the movie clip being attached to the movie clip.

depth An integer specifying the depth level where the SWF file is placed.

initObject (Supported for Flash Player 6 and later) An object containing properties with which to populate the newly attached movie clip. This parameter allows dynamically created movie clips to receive clip parameters. If initObject is not an object, it is ignored. All properties of initObject are copied into the new instance. The properties specified with initObject are available to the constructor function. This parameter is optional.

Returns

A reference to the newly created instance.

Description

Method; takes a symbol from the library and attaches it to the SWF file on the Stage specified by my_mc. Use MovieClip.removeMovieClip() or MovieClip.unloadMovie() to remove a SWF file attached with attachMovie().

You can extend the methods and event handlers of the MovieClip class by creating a subclass. For more information, see "Assigning a class to a movie clip symbol" in Using ActionScript in Flash.

Example

The following example attaches the symbol with the linkage identifier "circle" to the movie clip instance, which is on the Stage in the SWF file:

this.attachMovie("circle", "circle1_mc", this.getNextHighestDepth());
this.attachMovie("circle", "circle2_mc", this.getNextHighestDepth(), {_x:100, _y:100});

See also

MovieClip.removeMovieClip(), MovieClip.unloadMovie(), removeMovieClip()

Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 3:16 PM :
tvjunior said on May 3, 2004 at 11:47 AM :

Is there further information on attachMovie?

I have tried the example.
cutting and pasting most of the code.

I attached this code:
on (release) {
_root.attachMovie( "circle", "circle1", 2 );
}

to a button dragged from the components library, created a circ;e
movieClip and made certain it was in the library before the call.
Previous to this I tried to follow the ActionScript reference dictionary, and
the other Macromedia documentation. Please point out what I am
missing - I want the functionality, but I'm getting frustrated.

I've tested my failed fla.s on MacOS10.2/Flash MX and MacOS10.3/
Flash2004

Thanks
tim


netforce_tech said on May 10, 2004 at 6:09 PM :

Hi,

Have you export the cicle movieclip for actionscript ?
ie, did you right click on the circle movieclip in the library , select linkage and checked "Export for Actionscript" ?

netforce_tech
http://www.netforce.com.sg
TheJhereg said on Aug 31, 2004 at 2:37 PM :
One thing not mentioned is the return value if the call to <someclip>.attachMovie(...) fails. I'm assuming undefined for the moment, but would be good to know.

Reason: Say you were performing "this._parent.siblingMovie.attachMovie" in the onLoad of a AS2.0 Class. If the siblingMovie clip had yet to load, then it couldn't accept the attachMovie call -- and the result would be? ; )
nanodust said on Sep 17, 2004 at 5:58 PM :
the problem w/ attachmovie is that one must have a link identifier (in linkage panel) in order to attach it.

there is no way (that i know of) to attach a *dynamically* created clip - either by duplicateMovieClip or createEmptyMovieClip.

and there is no way to move a movieclip around the timeline by assignment, as far as i can tell.

so close...
Niels Peter said on Sep 20, 2004 at 4:35 AM :
Please note that the example:
this.attachMovie("circle", "circle2_mc", this.getNextHighestDepth(), {_x:100, _y:100});
will only work when exporting to FP7!
(Since 'getNextHighestDepth()' is for fp7 only).
No screen name said on Oct 6, 2004 at 1:42 AM :
Anyone know if the movieclip used in the attachmovie call must belong
in the current clip's library? (and not the library of an external swf)
It doesn't seem to attach them if this is the case.
jsoukup said on Oct 7, 2004 at 12:45 PM :
Is there a way to dynamically load a specific instance of a movie clip into another? attachMovie() appears to load a clip defined by linkage name directly from the library. What about the case where one dynamically creates a *specific instance* of a clip in library, modifys it then wants to display it in another blank movieclip?
chambio said on Nov 4, 2004 at 10:35 AM :
I've got the same problem

How attach a clip which is dynamically created ??

for example :

a.duplicateMovieClip(...)
// a is not in the library

i would like to do

b.attachMovie(a...)

could you help me ?
anm8tr said on Nov 9, 2004 at 9:16 AM :
initObject: Wrong?

Correct me if I'm wrong...

I've built a lot of code to be used for Flash 7. Now I'm creating a player for Flash 6, thinking that my code was largely backwards compatible.

It seems that in attachMovie for v6, the initObject:Object parameter does not work. I seem to recall not being able to get this to work at all in Flash MX.

It's a nice shortcut if you are v7 only.
phantompainter said on Nov 9, 2004 at 2:04 PM :
I've been trying to get the initObject to work in Flash 7 , but with no luck. I cant find a sample code that actually shows a working script, not just the syntax. I ve been using attachMovie and entering the name of the object (with commas) inside the [ , ] they show for the syntax but flash always picks up the comma inside the brackets as an error. Does the object have to be on the stage or off (I assume on!)
shimi2 said on Nov 9, 2004 at 3:41 PM :
The [square brackets] in the Usage section of the docs indicates that the parameter (and the comma) is optional.
In code, use {curly brackets} to pass an object with properties as a parameter. The comma is outside the curly brackets. Please see the Example; it works.
JohnGrden said on Nov 29, 2004 at 2:39 PM :
exactly:

var mc:MovieClip = this.attachMovie("id", "newInstanceName", depth, {_x:0, _y:450, _alpha: 50});
LUMENPHON said on Dec 1, 2004 at 12:15 AM :
Why bother to attach an exixting movie clip that is already created on stage (if the instance already exists on stage) if u want properties of dynamically created movie clip to follow an exixting mc then do this

on.enterframe = function()
{
mydynamicallycraetedclip._x = anexixting clip._x;
mydynamicallycraetedclip._y = anexixting clip._y;
mydynamicallycraetedclip._rotation = anexixting clip._rotaion;
...
...
}

it will follow whatever.
Fumio Nonaka said on Dec 5, 2004 at 2:02 AM :
The returned value of the newly created MovieClip instance should be
supported for Flash Player 6 and later.
Triynko said on Dec 7, 2004 at 12:24 PM :
The init object for attachMovie doesn't work sometimes.

this.scrollbar.attachMovie( "scrollbarbutton", "scrollup", 0, {_x:0,_y:0} );
this.scrollbar.attachMovie( "scrollbarbutton", "scrolldrag", 1, {_x:0,_y:25} );
this.scrollbar.attachMovie( "scrollbarbutton", "scrolldown", 2, {_x:0,_y:this.scrollbarheight-25} );

All three of these clips show up at the same location. If I add a line like "this.scrollbar.scrollup._x = 20;" it moves it to the correct location. So why isn't the initobject parameter working in flash 7?
AinCwain-Piska said on Jan 1, 2005 at 5:36 AM :
hi
i have a movie clip "mc" containing some function, say:

myFunction=function(myWord){
trace(myWord);
}

when im using mc.attachMovie("mc",mc,1); and then call my functiion it doesnt work, when i do trace(mc.mc) it gives me location of movie clip, but when trace(mc.mc.myFunction) it says undefined whereas it sud say [function type]...when i attach mc and then do "trace(mc.mc.myFunction)" from other function (or just bit later in the code) it works..the question why is it so? and what is the solution for this? thanx
Francis Cheng said on Jan 6, 2005 at 8:40 AM :
Hi Triynko,
One possibility is that you are trying to attach a button instead of a movie clip. Please check to make sure that the symbol you are attaching is a movie clip and not a button. I can only reproduce your problem if I try to attach a button instead of a movie clip. If you need further help, you'll probably have better luck in the forums: http://webforums.macromedia.com/flash
Francis Cheng said on Jan 6, 2005 at 9:00 AM :
Hi AinCwain-Piska,
You have a timing issue. The code you wrote will all be executed before the first frame of the movie is played, and the function you are trying to trace isn't available until after your attached "mc" movie clip is loaded. Attached movie clips usually become available in the first frame because the default setting in the Linkage properties is to export the movie in the first frame. You can work around this by delaying the call to your function until after your movie clip is loaded. One way to do this is to use an onEnterFrame handler, which will execute after you enter the first frame. The onEnterFrame handler will execute every time you enter a frame, so if you don't want the trace to loop, you'll want to delete the handler:

mc.mc.onEnterFrame = function ()
{
trace(mc.mc.myFunction);
delete this.onEnterFrame;
}
No screen name said on Jan 24, 2005 at 2:03 PM :
As anyone tried to apply a preload to a Movie.attachMovie call? Is it even possible? If not, should I put a preloader inside the attached Movie.
Richard Moss said on Jan 31, 2005 at 8:30 AM :
i can't get this piece of code to work what I am trying to do is just load a library mc out of the library and on to the stage. using attachMovie: - here is my code.

_root.attachMovie("buttonholder", "_root.newbuttonholder", _root.getNextHighestDepth(), {_x:477, _y:172});


I believe this should work - I have read that buttons cannot be loaded - this is a movieclip containing buttons? Any thoughts?
Francis Cheng said on Feb 2, 2005 at 9:42 PM :
Hi Richard,

Yes, that should work. Be sure to set the linkage identifier for your library movie clip to “buttonholder” (context-click on the symbol in the library, choose Linkage…, check “Export for ActionScript” and “Export in First Frame”, then type “buttonholder” in the Identifier text input area).
vyieort said on Feb 8, 2005 at 10:01 AM :
Concerning the init object with attachMovie -

using properties like _x and _y work fine but when I attempt to pass values for _height and _width they always result in 0 and 0, respectively. Is the initObject unable to apply sizing properties?
chi chi latté said on Feb 16, 2005 at 2:52 PM :
I was having problems attaching symbols into mcs loaded dynamically using loadMovie().

Somebody's written above that it's not too easy...

>> No screen name said on Oct 6, 2004 at 1:42 AM :
>>
>> Anyone know if the movieclip used in the attachmovie call must belong
>> in the current clip's library? (and not the library of an external swf)
>> It doesn't seem to attach them if this is the case.
>>
>>JohnGrden said on Nov 5, 2004 at 7:44 AM :
>> Yes, the linked clip MUST be in the library of the clip calling >>attachMovie.

Well you can do it without too much bother.

If you are using loadMovie(), and you're attaching clips into it and out of it, just make sure the symbol is in both libraries (being exported in one swf, and imported in the other), and place the imported symbol onto the stage of the swf which does not have the symbol native to its library. That make sense?
Note: you _have_ to place it on the non-native stage. Setting it as "imported" in the library is not enough. I put them hidden for one frame.

This is really useful if you've got a big project of several swfs, and you want to attach some common mcs into the loaded swfs.

Hope that helps+is not too garbled
IntensityLevel said on Feb 17, 2005 at 12:12 PM :
Is there a way to get ahold of the symbol with actionscript and change things in it that would then change all instances of it currently displayed in the movie?
What I mean is this... say pre run-time you export a symbol named "test" and subsequently at run-time, use attachMovie to put several instances of it on the stage. Is there a way to add a fill to "test" which would then show up in each instance that had been attached? How would we target this "prototype" sort of movieclip? _root.test wouldn't work of course, but I'm sure theres a way to get at it somehow, though its probably unpublished.
ilusi said on Feb 17, 2005 at 8:42 PM :
Hi. has anyone tried to unload (unloadMovieClip( )) movie clips that are created on a different movie file in which an object loads the movie clips (loadClip( ))? How do you target the object and its movie clips?

I have not had a successful attempts...
Guysh said on Feb 20, 2005 at 6:03 AM :
I have implemented a class which loads a MovieClip. This class is instantiated in one SWF (called 'a') which is loaded into a ScrollPane in another SWF ('b').
In SWF 'a' I initiate the class by passing 'this' (i.e the timeline in SWF 'a' to the class to attach the MovieClip to this timeline - atleast thats what I hope it would do)
In my first attempt I saw nothing.
Second attempt I imported the symbol to SWF 'b' - the MovieClip appeared but NOT in the scrollpane - not in SWF 'a'.
Third attempt - I attached the MovieClip in the first frame of SWF 'a' using:
stop();
attachMovie(<symbol name>, .....) - this time the MovieClip did appear in the right place!!!

How do I get to dynamically load MovieClip in one SWF and load that SWF in another SWF? - an keeping proper class encapsulation (I don't want the SWF to handle the MovieClip used by the Class)
No screen name said on Feb 21, 2005 at 2:25 PM :
In my Movie Clip y have buttons that sends me to another Scene, but it doesnt seem to work.... i've repleaced the string with a simple stop, and it worked great... but i cant make it work with a Gotoandplay.
Can anyone help me?
mspres said on Mar 10, 2005 at 12:57 PM :
Note: attachMovie() WILL work with shared asset symbols unless the movie is loaded into another movie. For example, consider that assetsUser.swf uses attachMovie() on shared asset symbols imported from assets.swf. This works fine until a third movie, assetsUserLoader.swf is used to load assetsUser.swf. Preloading the assets.swf with assetsUserLoader.swf does not solve the problem. The only solution is to include design-time instances of the shared asset symbols on assetsUser.swf stage; which, defeats the reasoning for using attachMovie.

The root of this bug and its various symptoms span across many topics.
No screen name said on Mar 19, 2005 at 2:24 AM :
Is there a way to refer to a linked movieClip (its properties, _x,_y etc.) which is associated with a class from the class it self? Even after dynamically creating it?
Is there a way to pass parameters to a linked movieClip's class when dynamically creating it?
Can anyone help please?
Thanks in advance.
No screen name said on Mar 29, 2005 at 8:47 PM :
Hey,

How can you populate a dynamic text element inside a movieClip that has a dynamically create instance name?

Below I have successfully attached a movieClip (with a dynamically created instance name) and wish to populate a text field (nameText) that resides within it.

** START CODE ******************************

msgFrom = mesg.substr(mesg.indexOf('(') + 1, mesg.indexOf(')') - mesg.indexOf('(') - 1);
movName = 'private_' + msgFrom;
_root.attachMovie("mc_privChatWin", movName, 9879);
_root.movName.nameText.htmlText = msgFrom;


** END CODE ******************************

Thanks !
No screen name said on Apr 8, 2005 at 9:23 PM :
Hi there. Can someone explain to me why this code doesn't work on
flash 7. Sorry i'm new on this. The intention is to duplicate movieclips
random, simulating rain drops.

count += 1;
if (count%2) {
_root.rain_mc.attachMovie("drop", "d"+count, count);
} else {
_root.rain_mc.attachMovie("drop1", "d"+count, count);
}
eval("d"+count)._x = Math.floor(Math.random()*600)+10;
eval("d"+count)._y = Math.floor(Math.random()*450)+10;
eval("d"+count)._rotation = Math.floor(Math.random()*360);
eval("d"+count)._alpha = Math.floor(Math.random()*45)+45;
if (count>12) {
remNum = count-12;
removeMovieClip(eval("d"+remNum));
}
No screen name said on Apr 15, 2005 at 4:22 AM :
Im Trying to check wich linkage is corrently load into a movie, how can i do that? any ideias?

ex:
this.attachmovie('pressed', 'icon',2 );
ShawnJC said on Apr 17, 2005 at 12:29 PM :
I've noticed a lot of use of the attachMovie command because it's easy to place pre-made movie clips inside other clips, however the downfall of this is that it doesn't show up in the totalBytes for the movie when trying to make a pre-loading bar. The workaround I have used for this is to place an instance of the clip you will be attaching on the stage, set it's visiblility to false, and uncheck the export in first frame from the linkage. That way you can still use attachmovie, and your pre-loader will function correctly.
hg2005 said on Apr 18, 2005 at 3:31 PM :
Hi guys,

I'm having the same issue, when I pass the initObject with _width, _height properties to duplicateMovieClip, it doesnt' seem to have any affect on the width & height of the duplicated movie clip . Instead, I get back 0 _width & _height values for the duplicated instance. Any body know why this is?


vyieort said on Feb 8, 2005 at 10:01 AM :
Concerning the init object with attachMovie -

using properties like _x and _y work fine but when I attempt to pass values for _height and _width they always result in 0 and 0, respectively. Is the initObject unable to apply sizing properties?
JohnGrden said on May 3, 2005 at 4:08 AM :
CORRECTION FROM EARLIER POSTS:

You CANNOT attachMovie with assets from a dynamically loaded library to any other timeline than where the library was loaded.

IE:

Load your swf into _level0.mc1.mc12

you CAN attach to these locations:
_level0.mc1.mc12
_level0.mc1.mc12.mc123
_level0.mc1.mc12.mc124
_level0.mc1.mc12.mc125

you CANNOT attach from this loaded library to these locations:
_level0
_level0.mc1
_level0.mc2
_level0.mc2.mc21
and so on....

I apologize for the confusion, I've had at least one email a month asking me how I *did* this - I don't do it, I work around it ;)

my previous post was incorrect and at the time, I was working with attaching audio from dynamically loaded clips and prototyping functions TO that loaded clip so that I could attachSound - IE: I got confused ;)
JohnGrden said on May 3, 2005 at 4:12 AM :
Someone just asked about seeing which SWF was loaded in. You can use the _url property of any movieclip to see it's file name and location.

You can also use the AdminTool for this at runtime:
http://acmewebworks.typepad.com/

see your Flash application in a treeview snapshop, hover over movieclips and the tooltip will show you all the properties - including _url. I use it ALL the time to verify that the correct swf was loaded.
Daniel wikstén said on May 16, 2005 at 3:32 AM :
AttachMovie does not return a reference to the new instance if you have an IF statement in the contructor (or something called by the contructor) of the class you want to instansiate. Nice undocumented bug... :)
Victmo said on Jun 14, 2005 at 2:28 PM :
Is it possible to attach a library asset into a dynamically loaded movie?

clipLoader.loadClip ( "dyn.swf", newMovie ); // url , target
newMovie.attachMovie( "lib_asset", "newName", 100);

Where `lib_asset` is a linked asset from my main movie's library

I heard this can be done with RLSs, but I don't think I have to get in so much trouble for such a simple operation

Cheers

Vic
JimBean007 said on Jun 27, 2005 at 10:29 AM :
Is there any way that you (Macromedia) could link this thread MovieClip.attachMovie() to the forums section of your website. It seems everyone is posting questions and looking for examples but it's a bit of a mess right now trying to locate answers.
No screen name said on Jul 6, 2005 at 6:36 PM :
Ok, I want to use attachMovie, but after the movie is "attached", I want the timeline to move to frame 2. Perhaps I should use _currentframe in an if statement. Don't know. Any thoughts?
No screen name said on Jul 6, 2005 at 6:38 PM :
Ok, I want to use attachMovie, but after the movie is "attached", I want the timeline to move to frame 2. Perhaps I should use _currentframe in an if statement. Don't know. Any thoughts?
James McNess said on Jul 8, 2005 at 8:01 AM :
hg2005, regarding setting _width and _height values:

You could try using _xscale and _yscale instead, although this is obviously not giving you the same sort of control at least it works!
alec mce said on Jul 20, 2005 at 3:54 AM :
There is a problem with the return value of this function. I run code which uses attachMovie on two different kinds of class. When I use attachMovie to add class A the code below works and both the implicit and explicit references work. When I use attachMovie to add class B, only the implicit reference works, the explicit reference traces null.

var str:String = "identifier";
var mc:MovieClip = tools.attachMovie(str,str,tools.getNextHighestDepth(),{_x: -50});
trace("implicit: "+tools[str]);
trace("explicit: "+mc);

I would recommend using the following code:

var str:String = "identifier";
tools.attachMovie(str,str,tools.getNextHighestDepth(),{_x: -50});
var mc:MovieClip = tools[str];

rather than

var str:String = "identifier";
var mc:MovieClip = tools.attachMovie(str,str,tools.getNextHighestDepth(),{_x: -50});

If anyone has any idea why this might work, I'd love to hear it...
RALPH79 said on Jul 29, 2005 at 6:02 AM :
Ok here is the deal. I’ve a movie with a combo box, an alert dialog and a datagrid. If I load this movie inside an application with forms there are any problems. But suppose I load this movie inside a normal flash document with the attachMovie method, if you click in the combo box, the dropdown will be underneath the datagrid. The same happens with the alert dialog (alert class with show method). Does anybody have some idea for these problems? How can I fix this?
Thanks,
astro_sk said on Jul 29, 2005 at 8:44 AM :
Senario:
you have an swf named "a.swf" which is loading into it "b.swf".
"a.swf" has in its library symbol "box" with linked id of "box".

now in "b.swf", which is being loaded using loadMovie on a, there is no items in its library and only one line of code:
_level0.attachMovie("box","box",2);//this works
_level0.my_mc.attachMovie("box","box",2);//this does not work
(on FP7)
is there any other way to have the needed code only load after the swf has been published??
AbdKanan said on Aug 5, 2005 at 3:19 AM :
_root.attachmovie("mc","new_mc",100);

it dont work if i wont export my mcs in 2nd frame. Help me please
eyestorm_ali said on Aug 20, 2005 at 10:30 AM :
RETURN is undefined REASON.

I had the same problem, I have been spending the last 3 hours trying to figure this out. I used what you mentioned and looked at my problem.

Here is what I had in a class that was linked to the Library MovieClip I was trying to attach to another MovieClip in the Scene.

function LogiAs(){
last_y = 0;
distance;
ParentPath = _parent;
nextDepth=0;
}

and I was getting undefined for the returning Instance Reference.
Guess what fixed it. distance; WAS THE PROBLEM. i changed distance; to distance=0; and then i got a returning value.

Now Maybe you don't have something like this in your code. But
maybe other things could cause the same problem. But this is for
anyone else that might have this problem and has made the mistake i've made.

Now not sure if this is a bug or what, but it sure has caused me a ton of waisted time.

Thanks Bye
No screen name said on Feb 8, 2006 at 7:23 PM :
Hi,
I noticed that if I use a depth of 100000 when attaching a clip, if I want to attach another above ite I cannot use any of the other 99999 depths under 200000 because Flash sorts them by the first digit.
_root.attachMovie("under", "under", 100000);
_root.attachMovie("above", "above", 100001);

The first one is above the second because it's loaded first. To fix this issue i had to change the depth of the "above" one to 200000.
_root.attachMovie("above", "above", 200000);

It took me a while to figure that one out...

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001487.html