AS3 - Get the child name from within itself (a bit like this.name):
我已经使用addChild()函数从Library中将MovieClip添加到了舞台。
子名称来自数组。
现在,我需要在添加的MovieClip中放置一段代码以查看自身,并返回它的"子代"名称,以便它可以运行一堆if语句等从内部进行配置。例如,它可以回顾informationWindows并根据自己的informationName提取数据
this.name将不起作用,因为我没有使用实例名称作为参考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // //The array within 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"} ); // ------------------------------------------ // // ------------------------------------------ // // ------------------------------------------ // // The code that adds the Info_win Movieclips and their Class from the Library to the main stage... var infoWinArray:Array = []; // Count the number of windows that will be called upon... var informationWindowCount:Number = simulator_constants.informationWindows.length; // Add the information windows to the Stage and dynamically give them names that we require... for (var i:Number=1; i<=informationWindowCount;i++){ infoWinArray[i-1] = new Info_win(); infoWinArray[i-1].name = simulator_constants.informationWindows[i-1]._informationName; addChild(infoWinArray[i-1]); } |
这是我问题的另一半,你们帮助解决了问题的最初部分。
不幸的是,我找不到一种方法来给孩子的实例名称加上孩子的名字。如果可以的话,我只需在有问题的MovieClip中运行this.name。
接收信息而不是自己获取
return what it's 'child' name is so that it can run a whole bunch of if statements etc. to configure itself from the inside.
这太复杂了,通常不是一个好主意。如现在所见,您使此数组静态化,以便从新创建的窗口"从内部"访问其元素。正如DodgerThud在评论中还建议的那样:与其给对象起一个名字然后在寻宝游戏中将其发送以查找必要的信息,不如直接给它提供信息。
我对以下代码进行了一些修改:
代码:
1 2 3 4 5 6 7 8 | var windows:Array = []; for(var i:uint = 0; i < informationWindowCount; ++i) { var window:InfoWindow = new InfoWindow(informationWindows[i]); windows.push(window); addChild(window); } |
如您所见,整个对象(数组元素)被传递给构造函数,这使构造函数可以"从内部配置自身"而无需从某个地方获取信息。
按照定义将对象传递给构造函数的方式通常称为配置对象。
为您的配置对象创建一个类
泛型对象既快速又容易,但是也同样迅速而容易地失败。
如果为他们创建一个类,则可以
- 检查值是否有效
- 设置默认值
- 为类型创建常量
-
添加更多功能(如序列化/反序列化)
例)
这样的类可能看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package { public class WindowConfiguration { public static const TYPE_1:uint = 1; //your types should have more descriptive names public static const TYPE_2:uint = 2; private static const TYPES:Array = [TYPE_1, TYPE_2]; public static const IMAGE:String ="image"; private var name:String; private var type:uint; private var x:Number; private var y:Number; private var title:String; private var subtitle:String; private var media:String; private var text:String; public function WindowConfiguration(name:String ="testPopUp", type:uint = TYPE_1, x:Number = 0, y:Number = 0, title:String ="Pop-Up Window", subtitle:String ="", media:String = IMAGE, text:String ="") { this.name = name; if(TYPES.indexOf(type) == -1) throw new ArgumentError("invalid type:" + type); this.type = type; // etc } public function get name():String { return name; } public function get type():uint { return type; } // etc } } |
完成此操作后,您的配置对象数组将变为以下内容:
1 2 3 4 5 | var windowConfigurations:Array = [ new WindowConfiguration("testPopUp", WindowConfiguration.TYPE_1, 0, 0,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here"), new WindowConfiguration("anotherTestPopUp", WindowConfiguration.TYPE_2, 100, 100,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here"), new WindowConfiguration("oneLastPopUp", WindowConfiguration.TYPE_1, 200, 200,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here") ]; |
整个代码看起来像这样。请注意,我更改了一些变量名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var windowConfigurations:Array = [ new WindowConfiguration("testPopUp", WindowConfiguration.TYPE_1, 0, 0,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here"), new WindowConfiguration("anotherTestPopUp", WindowConfiguration.TYPE_2, 100, 100,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here"), new WindowConfiguration("oneLastPopUp", WindowConfiguration.TYPE_1, 200, 200,"Example Pop-Up Window","Something Happened", WindowConfiguration.IMAGE,"window text here") ]; var windows:Array = []; for(var i:uint = 0; i < windowConfigurations.length; ++i) { var window:InfoWindow = new InfoWindow(windowConfigurations[i]); windows.push(window); addChild(window); } |
其他想法