dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: adding objects to main timeline with code AS3

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    51

    adding objects to main timeline with code AS3

    Hi...i have a container movie clip that holds several movie clips inside...

    I need to find out how i can add those same movie clips on the main timeline with code...

    So i need to figure out how to take objects from the library and add them on stage (main timeline) using code...


    Any ideas??

  2. #2
    AKA [ Paul Bainbridge] webdreamer's Avatar
    Join Date
    Aug 2001
    Location
    Glasgow, Scotland, UK
    Posts
    3,320
    Check out addChild() in the flash docs.
    It's fairly simple
    .: To me AS is like LEGO, Only for the big Kids :.
    - Site - Blog - Twitter - Linkedin
    bringmadeleinehome.com

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To get an instance of something in your library, you need to associate that library symbol with a class. Then, you can get an instance with the new operator. Let's say you give some symbol a classname of Bob.
    Code:
    var b:Bob = new Bob();
    Now, you have an instance of a Bob, but it's not on the screen yet. To add it to a DisplayObjectContainer, use addChild
    Code:
    someContainer.addChild(b);
    Where someContainer could be root (after casting to MovieClip or DisplayObjectContainer) or any other appropriate container instance.

  4. #4
    Member
    Join Date
    Nov 2010
    Posts
    51
    Thanks a lot guys..!!

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    51
    Hi again..tried it but i must be doing something wrong! can you guys havea quick look?

    see attachment

    Thanks!!
    Attached Files Attached Files

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Nope. No Flash here to open fla files.

    Post your code, and your error messages.

  7. #7
    Member
    Join Date
    Nov 2010
    Posts
    51
    Scene 1, Layer 'Layer 1', Frame 1, Line 2 1120: Access of undefined property Container.

    Scene 1, Layer 'Layer 1', Frame 1, Line 1 1180: Call to a possibly undefined method Class_Mc_1.

    Scene 1, Layer 'Layer 1', Frame 1, Line 1 1046: Type was not found or was not a compile-time constant: Class_Mc_1.


    What i did i created a movie clip named mc_1 and created a class for it named Class_mc_1 (right clicked it on the library and went to properties to do it)


    the code on main timeline is this


    var b:Class_Mc_1 = new Class_Mc_1 ();
    Container.addChild(b);

    I think is the way i created the class...

    Thanks again

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    From what I remember, you seem to have followed the steps to associate a class with a symbol properly. Did you get a message like "No class Class_Mc_1 found, one will be created"? You should have.

    The latter two errors would be caused by flash not knowing what Class_Mc_1 is, which would be the case if somehow the class didn't get created.

    The first error is because Container is undefined. Do you have an instance called Container?

  9. #9
    Member
    Join Date
    Nov 2010
    Posts
    51
    Hi....it worked... you were right about the container instance..that was a stupid error....

    The class was weird.... i tried different names and none of them worked...only until i removed the number from the class name..! Instead of Class_mc_1 i made it Class_mc I dont know why it doesnt like numbers...

    Thanks for all you help buddy...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center