Flash: setting a Flash document's instance name
I have a simple Flash document with a single rectangle on a stage. I've made the rectangle a symbol, a MovieClip,开发者_StackOverflow and set its instance name to "test_symbol_name". I've published this document to a .swf. (Here's the .fla and .swf)
In an AS3 Flash Builder project I load the swf with a Loader, then trace everything on it's display list with this function:
public static function traceDisplayList(container:DisplayObjectContainer, indentString:String = "->"):void {
var child:DisplayObject;
for (var i:uint=0; i < container.numChildren; i++) {
child = container.getChildAt(i);
trace(indentString, child.parent.name + " " + indentString + " " + child.name);
if (container.getChildAt(i) is DisplayObjectContainer) {
traceDisplayList(DisplayObjectContainer(child), indentString + "->");
}
}
}
Here's the output:
-> instance471 -> instance472
->-> instance472 ->-> instance473
->-> instance472 ->-> test_symbol_name
->->-> test_symbol_name ->->-> instance474
My symbol has become nested inside instance472, which is inside instance471.
I can reach the symbol in code by specifying the containing symbol (472), like this:
var myTestSymbol:MovieClip = (loader.getChildByName('instance472') as DisplayObjectContainer).getChildByName('test_symbol_name') as MovieClip;
Here's my questions
What are instance472 and instance471? Are they the document and the stage, or something else?
When I publish the swf, the instance number can change. I have to adjust my code to match the latest number to get it to work. How can I name instance471?
Is this the right approach, or is there a better way to reach my symbol instance?
You could use getChildAt(0) instead of that random name (afaik it's the swf's container). But I think the safest way would be to reference your clip as a documentClass member (a public variable) or if you need it for use within your parent movie, export your symbol for actionscript and make a new instance from your parent movie after you finish loading it:
loader.contentLoaderInfo.addEventListener("init", contentReady);
function contentReady(e) {
var clipdef:Object=loader.contentLoaderInfo.applicationDomain.getDefinition("MyClip");
var myClip:MovieClip=new clipdef() as MovieClip;
addChild(myClip);
}
精彩评论