AS3 dynamically attach MovieClip from a library to the Stage

强颜欢笑 提交于 2020-01-05 19:36:52

问题


I really have searched high and wide and i'm starting to think it may be my terminology that is preventing me from finding the answer.

I have a MovieClip in my library set to Export to ActionScript 3.0 with a Class called 'Info_Win' (its basically going to be a pop-up window, the contents of which depends on it's instance name).

I will be adding a given number of these MovieClips to the main stage and each one has to be given a custom instance name that will allow me to access it once it's on the stage (an array lays out the instance names). As previously stated, the Movieclip Instance name will then define what the contents of the pop-up window will be.

Here is the array:

static public var informationWindows:Array = new Array(     
    {_informationName:"iwPanelDescription", _popupType:"3"},
    {_informationName:"iwHelpConsole", _popupType:"2"},
    {_informationName:"iwExampleInstanceName", _popupType:"1"}
);

Running through the array and returning the _informationName should be straightforward.

Where I am tripping up is actually controlling the name of the MovieClip Instance names. In AS2 I would have wrapped an eval() argument around the left hand side of the argument.

I am using the following code: to create add the clips but this is no good:

var info_win:Info_win = new Info_win();
addChild(info_win);
info_win.name = "iwPanelDescription";

From here, if I can get the instance name issue sorted then I can use a simple for loop to access the array I have created and set the instance name up dynamically.

Have tried things like this but with no joy:

var mcName:String = "iwPanelDescription";
var this[mcName]:Info_win = new Info_win();
addChild(this[mcName]);

Anything that can be done to help would be most gratefully received!


回答1:


Have you considered storing your own reference vs relying on the stage?

You could do this easily like so:

var panels:Object = { };

panels.iwPanelDescription = new Info_win();
addChild(panels.iwPanelDescription);

trace(panels.info_win); // Get a reference via panels.

And to expand on how this could be used with your informationWindows array:

for each (var i:Object in informationWindows) {
    trace( panels[i._informationName] );
}

If you wanted to create these instances dynamically you can use getDefinitionByName() like so:

var panelClass:Class = getDefinitionByName('Info_win') as Class;
var info_win:Info_win = new panelClass();

addChild(info_win);



回答2:


What is the problem with

var mcName = "iwPanelDescription"
info_win.name = mcName;

From here, if you need the iwPanelDescription movie clip, you can just go ahead and

var mc = this.getChildByName(mcName);

And you're done.




回答3:


Solved...

Simulator Class...

package 
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;
    import flash.external.ExternalInterface;

    public class Simulator extends MovieClip
    {
        public static var informationWindowCount:Number = simulator_constants.informationWindows.length;
        public static var currentWindowIndex:String = "0";
        public function Simulator()
        {
            addInformationWindows();


        }
        public function addInformationWindows() {
            var infoWinArray:Array = [];
            // Count the number of windows that will be called upon...

            // Add the information windows to the Stage...
            for (var i:Number=1; i<=informationWindowCount;i++){
                infoWinArray[i-1] = new Info_win();
                infoWinArray[i-1].name = simulator_constants.informationWindows[i-1]._informationName;
                this.addChild(infoWinArray[i-1]);
                currentWindowIndex = String(i);
            }
        }
    }
}

simulator_constants Class...

package 
{
    public class simulator_constants
    {

        static public var informationWindows:Array = new Array(
            {_informationName:"testPopUp", _popupType:"1", _x:"0", _y:"0", _title:"Example Pop-Up Window",  _subtitle:"Something Happened", _media:"image", _text:"window text here"},      
            {_informationName:"anotherTestPopUp", _popupType:"2", _x:"100", _y:"100", _title:"Example Pop-Up Window",  _subtitle:"Something Happened", _media:"image", _text:"window text here"},
            {_informationName:"oneLastPopUp", _popupType:"1", _x:"200", _y:"200", _title:"Example Pop-Up Window",  _subtitle:"Something Happened", _media:"image", _text:"window text here"}
        );

    }
}

Info_win Class...

package  {

    import flash.display.MovieClip;


    public class Info_win extends MovieClip 
    {
        public static var informationName:String = Simulator.currentWindowIndex;
        var info_win_panel_content_title:Info_win_panel_title = new Info_win_panel_title();
        var info_win_panel_content_content:Info_win_panel_content = new Info_win_panel_content();




        public function Info_win() {
            //Get details on this information window...
            var _informationName = simulator_constants.informationWindows[Simulator.currentWindowIndex]._informationName;
            var _popupType = simulator_constants.informationWindows[Simulator.currentWindowIndex]._popupType;
            var _x = simulator_constants.informationWindows[Simulator.currentWindowIndex]._x;
            var _y = simulator_constants.informationWindows[Simulator.currentWindowIndex]._y;
            var _title = simulator_constants.informationWindows[Simulator.currentWindowIndex]._title;
            var _subtitle = simulator_constants.informationWindows[Simulator.currentWindowIndex]._subtitle;
            var _media = simulator_constants.informationWindows[Simulator.currentWindowIndex]._media;
            var _text = simulator_constants.informationWindows[Simulator.currentWindowIndex]._text;

            //Add the standard componoents for all windows...
            this.name = _informationName;
            this.x = _x;
            this.y = _y;

            //Conditional construction to happen in here...

            //components
            addChild(info_win_panel_content_title);
            addChild(info_win_panel_content_content);
            //positioning of components...
            info_win_panel_content_content.y = info_win_panel_content_title.height;
            trace(this.width);



            //Outputs for debug...
            /*trace("_informationName: "+_informationName);
            trace("_popupType: "+_popupType);
            trace("_x: "+_x);
            trace("_y: "+_y);
            trace("_title: "+_title);
            trace("_subtitle: "+_subtitle);
            trace("_media: "+_media);
            trace("_text: "+_text);
            trace("------------------------------");
            trace("------------------------------");
            trace("   ");*/
        }
    }

}


来源:https://stackoverflow.com/questions/31784163/as3-dynamically-attach-movieclip-from-a-library-to-the-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!