A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: create movieclip in anothher class

  1. #1
    Member
    Join Date
    Jun 2006
    Location
    Adam
    Posts
    78

    Cow Icon create movieclip in anothher class

    Hi everybody...
    I'm pretty new in AS3...

    In class A I write
    Actionscript Code:
    [...]
    public class A extends MovieClip
        {
            public function A():void {

            public static var menuContainer:MovieClip;
                    menuContainer = new MovieClip();
            addChild (menuContainer);
            menuContainer.y = 200
    [...]


    In class B I write

    Actionscript Code:
    [...]
    var p:menuItem_mc = new menuItem_mc()
    Main.menuContainer.addChild (p)
    [..]

    And this works fine.

    But I would like to move all the code belonging to the menu to class B...
    so I commented all the code above in class A and in class B I wrote:
    Actionscript Code:
    var p:menuItem_mc = new menuItem_mc()
    var Main.menuContainer:MovieClip;                    
    Main.menuContainer.addChild (p)

    but I got the following error:
    TypeError: Error #1010: A term is undefined and has no properties.
    at Menu$/deployMenu()

    So...my question is: how can I create a movieclip in another class and add a child to it?
    thanks,
    Adam
    _______________
    Adam

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There is a lot wrong with the code you posted. It shouldn't work at all. You've got a public static variable declared inside the constructor for A. That shouldn't be valid.

    Anyway. You cannot declare variables/properties in one class from another. Nor should you need to. If A was dynamic, you could add an arbitrary property to an instance of A at runtime, but not a static property to A itself.

    What you should be doing here is creating a Menu class, then create an instance of that Menu in B. Prepare the Menu instance with any menuItems you need to, then you can use addChild to add the instance of Menu to any other DisplayObjectContainer instance. I'd suggest adding it to your instance of B since B seems to know everything about and require the menu. But if your instance of B has a reference to an instance of A, you could add the menu to that.

  3. #3
    Member
    Join Date
    Jun 2006
    Location
    Adam
    Posts
    78
    Quote Originally Posted by 5TonsOfFlax View Post
    There is a lot wrong with the code you posted. It shouldn't work at all. You've got a public static variable declared inside the constructor for A. That shouldn't be valid.

    Anyway. You cannot declare variables/properties in one class from another. Nor should you need to. If A was dynamic, you could add an arbitrary property to an instance of A at runtime, but not a static property to A itself.

    What you should be doing here is creating a Menu class, then create an instance of that Menu in B. Prepare the Menu instance with any menuItems you need to, then you can use addChild to add the instance of Menu to any other DisplayObjectContainer instance. I'd suggest adding it to your instance of B since B seems to know everything about and require the menu. But if your instance of B has a reference to an instance of A, you could add the menu to that.
    Yep...got it.
    Thanks for helping
    _______________
    Adam

Tags for this Thread

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