View comments | RSS feed

attachMovie (MovieClip.attachMovie method)

public attachMovie(id:String, name:String, depth:Number, [initObject:Object]) : MovieClip

Takes a symbol from the library and attaches it to the movie clip. Use MovieClip.removeMovieClip() or MovieClip.unloadMovie() to remove a SWF file attached with attachMovie() method.

You can extend the methods and event handlers of the MovieClip class by creating a subclass.

Availability: ActionScript 1.0; Flash Player 5

Parameters

id:String - The linkage name of the movie clip symbol in the library to attach to a movie clip on the Stage. This is the name that you enter in the Identifier field in the Linkage Properties dialog box.

name:String - A unique instance name for the movie clip being attached to the movie clip.

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

initObject:Object [optional] - (Supported for Flash Player 6 and later) An object that contains 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.

Returns

MovieClip - A reference to the newly created instance.

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});

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also

removeMovieClip (MovieClip.removeMovieClip method), unloadMovie (MovieClip.unloadMovie method), removeMovieClip function


Version 8

Comments


McUsher said on Nov 23, 2005 at 9:48 AM :
If you use attachMovie the following way:
this.attachMovie("circle", "circle_instance", this.getNextHighestDepth());
and later again
this.attachMovie("circle", "circle_instance", this.getNextHighestDepth());

You will have 2 Instances of circle with the same name on different depths.
No screen name said on Nov 23, 2005 at 7:58 PM :
Hi
I am having a problem attaching movie clips from the library in flash 8,
everything worked fine in flash mx pro.
this is my code:
empty1_mc.attachMovie("world", "fArea", this.getNextHighestDepth());

empty1_mc in on the stage under a mask. why is this no longer attaching
to the clip on the stage?
Sim-Enzo said on Jan 1, 2006 at 3:09 PM :
Regarding McUsher's comment, I suspect that isn't what's happening. If you try to create a movieclip within the same scope of another movieclip of the same name, Flash automatcially adds a unique number at the end of the newer movieclip.
No screen name said on Feb 18, 2006 at 4:33 PM :
_root.attachMovie("ClipInLibrary", "AddedClip", 60);
AddedClip._x = 50;
AddedClip._y = 50;

_root..AddedClip.removeMovieClip();

_root.attachMovie("NextClipInLibrary", "AddedClip", 60);
AddedClip._x = 50;
AddedClip._y = 50;

worked fine in Flash 7.

In Flash 8, it works the first time but when attaching NextClipInLibrary, the positioning commands are ignored and AddedClip is placed at 0,0.

I stumbled across the alternative form:

_root.attachMovie("NextClipInLibrary", "AddedClip", 60, {_x:50, _y:50});

and this works properly.

Any ideas what is going on?

Thanks,

Jeff Davies
No screen name said on Mar 30, 2006 at 6:37 PM :
The mainCanvas is the primary movie i am working on. I have another movie clip symbol in the library with the button and a text box in it. When I attach it to the mainCanvas using attachMovieClip, the text in teh text box, and the button dissappear.
The button is just blank with no text on it
--thanks
rob.snider said on Apr 5, 2006 at 11:36 AM :
Try replacing;

empty1_mc.attachMovie("world", "fArea", this.getNextHighestDepth());

with;

empty1_mc.attachMovie("world", "fArea", empty1_mc.getNextHighestDepth());
reinier77 said on Apr 11, 2006 at 6:58 AM :
You can only attach movieclips from libraries of parents (up the hierarchy chain in respect to the movieclip that you want to attach the movieclip to), and not from libraries of children (down the chain).

But if you load another swf with a library into your movie with its own library, then you can only attach movieclips to the loaded swf that are in the library of the loaded swf. And you can only attach movieclips to the parent movie that are in the library of the parent movie.

So, it seems movies that are being loaded into a parent movie inherit the library of the parent, unless they have their own library...

This is very annoying if you want to create different library-swfs that will be loaded in your main movie, because you can never use all the loaded libraries in 1 single timeline (=movieclip).
There should be a solution to this problem, since it limits the use of loadMovie and attachMovie, and very much so in my opinion...

Does anybody know of a work-around? Using shared assets is not an option. Most of the time I don't know beforehand (while building the mainmovie) what linked items will be in the loaded libraries.
No screen name said on Oct 17, 2006 at 6:29 AM :
I´m devoloping a V2 component, which is a menu. I tried to create a movieClip inside it using createChild(MovieClip,"elemtentsContainer" ); that will be used to hold al the buttons instances , which are also V2 components.
the problem is that when I want to create an instance of the button inside the "elementsContainer" using attachMovie, the flash player gets locked like it where in an infinite loop.

If I use createEmptyMovieClip to create the buttons holder ,"elementsContainer", it doesn´t lock but elements are not displayed.

If I use createChild to create the menuitems componet in the root of the menuComponent the menu items are created perfectly.

what am I doing wrong?
No screen name said on Nov 19, 2006 at 5:06 PM :
Hi ,
Iam trying to use the followint statement ..

var MenuBar:MovieClip = _root.attachMovie (MenuBar.SymbolName , "MainMenu" ,_root.getNextHighestDepth() ,{_x:100 , _y:100});

This works but if I try to use
var MenuBar:MovieClip = _root.attachMovie (MenuBar.SymbolName , "MainMenu" ,_root.getNextHighestDepth() ,{_x:100 , _y:100,_width:1000,_height:100});

then the MenuBar is not visible .. What could be the reason ?
No screen name said on Dec 12, 2006 at 5:56 AM :
I used this format in my project to load a bunch of instances of 5 different movie clips onto the stage.

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

I can't seem to be able to unload them all to get to the end screen (at the end of the game, there are about 30 instances on the stage). How do I unload all those movie clips?
No screen name said on Jan 3, 2007 at 12:38 AM :
Having this problem:
_root.attachMovie("ClipInLibrary", "AddedClip", 60);
AddedClip._x = 50;
AddedClip._y = 50;

_root..AddedClip.removeMovieClip();

_root.attachMovie("NextClipInLibrary", "AddedClip", 60);
AddedClip._x = 50;
AddedClip._y = 50;

I used removeMovieClip(_root..AddedClip) instead of _root..AddedClip.removeMovieClip();

and everithing worked fine
bcraigie said on Feb 8, 2007 at 5:42 AM :
It seems there's an issue if you turn off "export in first frame" in a movieclip you are trying to load using attachMovie. You need to turn off "export in first frame" if you are using a preloader, and then you set the frame to export to 2 in publish settings, then do your attachmovie in frame 3. But that doesn't seem to work with attachMovie, and the movies just don't appear, and there doesn't seem to be a way to be notified when it's finished attaching, making attachMovie pretty much useless if you need to interact with the movie once it has been attached. Here's hoping Adobe come up with a solution (such as doLater() which only works for components, or an onInit() routine). If I've missed something here, it would be nice to have the docs updated to explain the proper way to handle these problems. :-)
nads said on Feb 15, 2007 at 5:57 AM :
Just a heads-up on what could be a "bug": make sure that any movieclip you want to attach is actually a Movieclip in the library, not e.g. a graphic (if so, change it in properties). I attached some graphics and got very bizzare results: static text changing, some parts hiding, etc. After changing the type of the libraryobject to "movieclip", all worked perfectly.
No screen name said on Apr 3, 2007 at 12:33 PM :
I think it would be helpful to know more about removing attached movie clips without expanding the memory usage. I made a simple program that attaches and removes movieclips, when it runs in the stand-alone flash player, I see the "mem usage" slowling increasing. Is there another step other than removemovieclip that is required?

stop();
numBalls = 0;
countUp = 0;
this.onEnterFrame = function(){
countUp++;
if (5 < countUp){
countUp = 0;
if(numBalls > 5) numBalls=1
else numBalls++;
if (eval("mb"+numBalls) != undefined)eval("mb"+numBalls).removeMovieClip()
else {attachMovie("movingball","mb"+numBalls,numBalls)
myName = eval("mb"+numBalls);
myName.myCount = 0;
myName._x = myName.x = random (400);
myName._y = myName.y = random (300);
}
}
}
maestudios said on Apr 16, 2007 at 11:13 AM :
noscreenname - remember that you should be using the depth manager as opposed to getnexthighestdepth when working with components. otherwise you'll get bizarre display errors (such as the one you're describing).
No screen name said on Apr 17, 2007 at 8:32 PM :
I'd like to call a .swf on another server from within a .swf on my web page.

I haven't seen any examples of this. Can I do this?

Marilyn
Max Damage said on Apr 19, 2007 at 2:17 PM :
How can I use (call, access) the objects that are stoerd in the library of an loaded swf file. Is it possible to do this?

I've seen that reinier77 has an similar question, but no answer.

Thank You!
Asianeklipse said on Apr 23, 2007 at 5:06 PM :
Hey guys, Well what i'm trying to do is create a little map where in the main area you click on a button and it opens a window. That part works fine, but I was curious how I can assign properties to the movie clips after I create them. For example.

song1Btn.onRelease = function () {
this.attachMovie("song_info", "song", this.getNextHighestDepth(), {_x:100, _y:100});
}

works fine, and it loads the movie clips fine. I just need a way to close the clips. in "song_info" there is a close button, but I'm not sure what code to put in it. I've tried

closeBtn.onRelease = function() {
this._parent.removeMovieClip();
}

closeBtn.onRelease = function() {
this.removeMovieClip();
}

and it's not working, so basically i need to know how to get the movie clip to close itself. something similiar to the windows found at www.theswak.com i want to create a system similiar to his without the double clicking. thanks.
Max Damage said on Apr 26, 2007 at 11:56 PM :
if the close button is located in the main timeline of the attached movieclip then this should work:

closeBtn.onRelease = function() {
this.removeMovieClip();
}

because on a button, the reference to the movieclip that contains the button is 'this'.

now, because you built this function, I'm not shure, but I think that 'this' refers to the object where you put the code.

I think you should try to put the code on the button and avoid creating this function, like this:

on(release){
removeMovieClip();
}

only if the button is contained directly in the main timeline of the movie that you want to remove, else you shoud use the appropriate path to the clip. anyway, you can always add an trace(this); trace(this._parent); just to see where exactly is located the button.
No screen name said on Apr 29, 2007 at 12:08 PM :
How does one use this syntax in a class? How do you reference the library symbol in a class?

this.attachMovie("circle", "circle_instance", this.getNextHighestDepth());
Bhupi_99000 said on Jun 13, 2008 at 4:32 AM :
how would I use the movieclip or textfield which is inside in a dynamically attached movie by the help of "attachMovie" method?

 

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

Current page: http://livedocs.adobe.com/flash/8/main/00002440.html