attachMovie() in AS3

Using external assets is great, but for some types of projects it makes sense to include your assets in the FLA as library symbols. According to this AS3 migration cheatsheet, attachMovie() has been removed in AS3. Does this mean we can no longer attach library symbols to the stage in AS3? Fear not flashers, you can still attach library symbols in AS3. Here’s how:

  1. In the Flash 9 IDE, create a new flash document.
  2. Create a new library symbol, call it “Test”. Draw some awesome graphics.
  3. Right click the symbol and open the symbol properties. Check the “Export in Actionscript” checkbox. By default Flash will use the symbol’s name as the Class name, and will warn you that a class for this symbol is to be automatically created. This class name replaces the old AS2 “linkage id”.
  4. On the main timeline, frame 1, add this code:

    [as]this.addChild(new Test());[/as]

    What this code is doing is creating an instance of your new class, and adding that instance to the main timeline’s displayList. (This new syntax is actually a lot more logical and consistent than the old attachMovie() syntax).

  5. Test your movie, see your awesome graphics.

To take this a step further you can associate some code with your new Symbol by creating an AS class file for it. Here’s how:

  1. Save the FLA from above somewhere.
  2. Create an AS file for your new symbol. To do this, create a text file using any text editor, or in the flash IDE with File->New->Actionscript File. Save the new file as “Test.as” in the same folder where your FLA is. (The AS file needs to be in the FLA’s class path. By default, the class path includes the current folder).
  3. Add your custom class code in the AS file and save it.

    [as]package{

    import flash.display.MovieClip;

    public class Test extends MovieClip{

    public function Test(){
    trace(“This code runs when the Test class is instantiated”);
    }
    }
    }[/as]

  4. Test your movie. You will see the trace in the output window..

Here is a good intro tutorial on using AS3 and Flash 9, which covers the above in more depth, as well as the new AS3 document class.

Note this information only applies when using the Flash 9 IDE, not Flex 2 , since Flex 2 doesn’t have a library.

30 Responses

  1. Billy says:

    So this solves the problem of “instantiating” a class associated with a movieclip? It took me a good while to bend my brain around the concept of equating attachMovie(“Test”) with new Test().

    Thanks!

  2. Adrian says:

    Hi,

    Wanted to say thanks for such a great script (SimpleViewer).

    Can you tell me…how to change the text that says “simpleviewer title” racking my brains trying to find it!!!

    I’m using it on a project here – http://www.haloseo.com/mojojewel/

    Kind regards

    Adrian.

  3. ip adres says:

    Thanks for this.

  4. Jacob says:

    this – this.addChild(new Test()); – works if you export classes on frame1 by default. What if I want to import my classes on frame 4 ? this does not seem to work anymore.

  5. Blackkite says:

    Thanks

  6. newton says:

    can you suggest me a method for attaching eelments in second frame by the help of any class where in the first fram ei have a preloader?

  7. saravanan says:

    HI,
    ur tuts are nice …. and very useful…

    i like to know that how you embed action script in the web pages.. could u tell me ..it would be very useful for my website i am developing..

    thankx in advance..

  8. DigSafe says:

    How do you then target the movieclip? It has no name.

  9. DigSafe says:

    OK…figured this out myself.

    Assuming you create a movieclip and then give it unique class (for this example it is ‘libraryItem_mc ‘) in the library, you can then instantiate it by doing the following;

    var myMovie:libraryItem_mc = new libraryItem_mc ();
    stage.addChild(myMovie);

    Now ‘myMovie’ can be targeted and it’s properties manipulated.

  10. Floris says:

    About the targeting of the movieclip that is created as shown in this tutorial and the naming question put forward by DigSafe: put the instance in a variable instead of just creating it:

    var myMovie = this.addChild(new Test());
    myMovie.x = 200;
    myMovie.y = 200;

    /* etcetera */

  11. Floris says:

    If you want to run a loop and name the child objects for later reference, you can use the ‘name’ property of the instance; and select it with the method ‘getChildByName()’.

    for (var i:int=0; i

  12. ricky says:

    thanks, and yes, because i had a lot of other objects in the script, i found it only worked using the method:

    var controlBar:control = new control ();
    stage.addChild(controlBar);

    (where ‘control’ is the class name added in the library)
    (this was for a video player application)

  13. Oyunlar says:

    1120: Access of undefined property stage.

    stage.addChild says this.

  14. […] Here you can find another explanation about this subject: by Airtight Interactive […]

  15. Sven says:

    That’s all fine. But how do you at a library item by name using a String?

    AddItem(“itemname”);

    function AddItem(id:String):void
    {
    /* Some code for making MovieClip or so … and then */
    addChild(id);
    }

  16. Novice says:

    Hi Sven

    I’m pretty new to this but I think this works and its what I’m trying to do as well:

    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    var ClassReference:Class = getDefinitionByName(“DataSymP”) as Class;

    var myMovie = this.addChild(new ClassReference);
    myMovie.x = 0;
    myMovie.y = 0;

    Where DataSymP is your symbol name in the library or whatever.

    Hope that helps.

  17. Chuck says:

    Hi Novice

    I need this too, but for your line:

    var ClassReference:Class = getDefinitionByName(”DataSymP”) as Class;

    I get the following error statement: 1093: Syntax error.

    Have you worked it out?

  18. ilmr says:

    Just replace the ”” with “”.

  19. Thomas says:

    fine, now I can attach MovieClips from the library, but how do I access nested Movieclips or Variables inside? Unsing dot-syntax throws the error:
    Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich

  20. Alik says:

    Very nice tutorial, thank you! Taking this one step forward, how do I monitor the playback on the loaded clip and remove it once it is finished playing so I can load a new movie clip in his place? Thanks!!!

  21. 1120: Access of undefined property stage.

    stage.addChild says this.

  22. KielSoft says:

    addChild(new MCinLibrabryWithLinkage()).name = “myMovie”;
    getChildByName(“myMovie”).x = 100;

  23. scottb says:

    Thanks for this write up. New to flash and was getting hung up on why my classes were not connecting to the objects in the Library. Simple concept really but the flash error messages were no help :)

  24. lignano says:

    Thanks for this solution
    Alby

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>