AS3将MovieClip从库动态附加到舞台

时间:2015-08-03 09:44:15

标签: arrays actionscript-3 instance dynamically-generated addchild

我真的在广泛搜索,我开始认为这可能是我的术语阻止我找到答案。

我的库中有一个MovieClip设置为导出到ActionScript 3.0,其中一个名为' Info_Win' (它基本上是一个弹出窗口,其内容取决于它的实例名称)。

我将在主舞台上添加一定数量的这些MovieClip,并且必须为每一个添加一个自定义实例名称,以便我可以在舞台上进行访问(阵列布局实例名称)。如前所述,Movieclip实例名称将定义弹出窗口的内容。

以下是数组:

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

遍历数组并返回_informationName应该很简单。

我绊倒的地方实际上是控制MovieClip实例名称的名称。在AS2中,我会在参数的左侧包含一个eval()参数。

我使用以下代码:创建添加剪辑但这不是好事:

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

从这里,如果我可以将实例名称问题排序,那​​么我可以使用一个简单的for循环来访问我创建的数组并动态设置实例名称。

尝试过这样的事情,但没有任何快乐:

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

任何可以提供帮助的事情都会非常感激!

3 个答案:

答案 0 :(得分:1)

您是否考虑过存储自己的引用而不依赖于舞台?

你可以这样轻松地做到这一点:

var panels:Object = { };

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

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

并扩展如何与informationWindows数组一起使用:

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

如果您想动态创建这些实例,可以使用getDefinitionByName(),如下所示:

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

addChild(info_win);

答案 1 :(得分:0)

有什么问题
var mcName = "iwPanelDescription"
info_win.name = mcName;

从这里开始,如果你需要iwPanelDescription影片剪辑,你可以继续

var mc = this.getChildByName(mcName);

你已经完成了。

答案 2 :(得分:0)

...解决

模拟器类......

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("   ");*/
        }
    }

}