A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Attach different movieclips with one function

  1. #1
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550

    Attach different movieclips with one function

    I'm starting to get the hang of AS3 and its crazy craziness, but I've hit yet another roadblock Im hoping someone can help me with.

    I'm adding movieclips from an external .as class called GUI. But what I'd like to do is be able to load multiple GUIs - lets say GUI0, GUI1 & GUI2 - through the one class using a dynamic variable. So if I need to load a certain GUI i can just instantiate the GUI class and run newGUI.loadGUI(GUIname); and have it load the correct movieclip based on the variable.

    So, syntax aside, it would look something like this:

    Code:
    loadMenu(GUIname) {
         GUItoLoad = GUIname;
         var mcLoader:GUItoLoad = new GUItoLoad();
         myStage.addChild(mcLoader);
         mcLoader.addEventListener(Event.ENTER_FRAME,GUItoLoad);
    }
    *myStage is reference to the root stage from inside the GUI class.

    I've tried a few different things syntax wise, and have had a search and browse, but have come up empty handed.

    Any help would be greatly appreciated!


    Brendan
    Skribble

    Its not that im lazy, I just dont care.

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    i mean , you could say
    Code:
    private function loadMenu( GUIname : Class ) : void {
         var mcLoader: Class = new GUIname();
         myStage.addChild(mcLoader);
         mcLoader.addEventListener(Event.ENTER_FRAME,GUItoLoad);
    };
    I don't know off hand if the compiler will throw a fit , because technically , mcLoader is a class , not a display object. So you might need to wrap mcLoader ,

    Code:
    myStage.addChild( GUIname( mcLoader ) );
    or you could try

    Code:
    myStage.addChild( mcLoader as GUIName );
    However , this is not best practice for several reasons. Most notably , theres no way to determine what , if any , additional arguments would need to be passed into the constructor. I understand you are probably in control of the object architecture , so you would explicitly know wether or not the class required arguments , but it might be a good idea to add a rest statement to the constructor of your class , like

    Code:
    public function SomeClass( ...args ) : void {
    
    };
    you also might consider having those gui classes implement some sort interface so they share a common api.

    or. you could just write a simple switch statement. yes its not as elegant as the former , but its just as effective , and unless you are comparing hundreds of different gui types , you will see no performance gain one way or the other.
    Last edited by AttackRabbit; 04-27-2010 at 10:50 PM.

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