【问题标题】:AS3 dynamically attach MovieClip from a library to the StageAS3 动态地将 MovieClip 从库附加到舞台
【发布时间】:2015-08-03 09:44:14
【问题描述】:

我确实搜索过很多次,我开始认为可能是我的术语阻止了我找到答案。

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

我将向主舞台添加给定数量的这些影片剪辑,并且必须为每个影片剪辑指定一个自定义实例名称,以便在它在舞台上时允许我访问它(一个数组列出了实例名称)。如前所述,Movieclip Instance 名称将定义弹出窗口的内容。

这是数组:

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

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

我遇到的问题实际上是控制 MovieClip Instance 名称的名称。在 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]);

我们将不胜感激地接受任何可以提供帮助的事情!

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

    • 从这里开始,我仍然停留在面板必须明确声明并且不是来自数组的位置。将有更多与类中包含的信息面板有关的数据。
    • @JonBoy2000 哦,我明白了,我没有从你的问题中明白这一点。你想要的可以使用getDefinitionByName 来完成,我现在将更新我的答案。
    • 嗨,Marty,感谢您的帮助。我遇到困难的地方是在定义的左侧定义变量名。我想做这样的事情...... panel.this[myArrayofInstanceNames[i] = new Info_win();该类本身将查看数组,当它找到匹配项时,它会查找它的弹出类型。然后从那里知道要在其内部填充哪些按钮和数据。如果没有实例名称,我无法使用 Info_win 类中的 this.name 来查找它的详细信息。
    • 我现在快到了!对不起,我还在处理这个网站! '代码' var infoWinArray:Array = []; var informationWindowCount:Number = simulator_constants.informationWindows.length; for (var i:Number=1; i
    【解决方案2】:

    有什么问题

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

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

    var mc = this.getChildByName(mcName);
    

    你已经完成了。

    【讨论】:

    • 感谢您的帮助。我已经尝试过了,我能够得到一个结果,但是我又回到了同样的问题,因为 'mcName' 和 'mc' 是静态变量名,需要动态声明。如何使 mc 动态声明?
    【解决方案3】:

    已解决...

    模拟器类...

    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 类...

    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 类...

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

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      相关资源
      最近更新 更多