Is there a way to access or iterate through a list of all objects exported to ActionScript from the Flash IDE? I'm looking to save myself some hassle by just iterating th开发者_如何学Pythonrough selected MCs and processing them in a certain way, without knowing their names.
Thanks.
I use the SwfClassExplorer described here: http://flassari.is/2008/07/swf-class-explorer-for-as3/
Not at run-time, no. You can manipulate library objects with JSFL in the IDE though: http://livedocs.adobe.com/flash/9.0/main/flash_cs3_extending.pdf. Not sure if that helps at all but perhaps you can generate code for use in your application by analyzing the library in some way.
var lib = fl.getDocumentDOM().library;
for (var i = 0; i < lib.items.length; i++)
{
var item = lib[0];
fl.trace(item.name + " " + item.getItemType());
}
Perhaps generate some code based on library objects' getItemProperty()
or getItemType()
.
Other than that, I think your best bet is to do as the others said. Create a dummy MovieClip that has each element inside of it and hide it off stage. Add a listener for "added to stage" on it and loop through its children and use "reflection" getQualifiedClassName to perform actions based on it's class or just use an instance name and a switch statement.
Lastly though, what is it exactly that you are "processing" on each of these MovieClips? Perhaps it's more of a design issue and they should all extend a common MovieClip subclass that has an "added to stage" handler added to it where you look at the MovieClip's type as it's added to your application and perform some actions in that single function. I'm working on some Localization work at work right now and this is how we handle processing multiple different clips at runtime.
This questions is somewhat similar to this one.
This is the JSFL you need to list the only the MovieClips that are exported to actionscript:
var doc = fl.getDocumentDOM();
var libItems = doc.library.items;
var libItemsNum = libItems.length;
var classesString = 'var '+doc.name.substr(0,doc.name.length-4)+'Classes = [';
var classesNum = 0;
var classes = [];
fl.outputPanel.clear();
for(var i = 0 ; i < libItemsNum; i++){
if(libItems[i].linkageExportForAS){
classes[classesNum] = libItems[i].linkageClassName;
classesNum++;
}
}
for(i = 0; i < classesNum; i++){
if(i < classesNum-1) classesString += '"'+classes[i]+'",';
else classesString += '"'+classes[i]+'"];';
}
fl.clipCopyString(classesString);
fl.trace(classesString);
It traces out the name of your exported classes as an array of strings so you can use that in with ApplicationDomain's getDefinitionByName() or flash.utils.getDefinition(). Also, it copies the traced message to the clipboard. If you save the JSFL in the Commands folder, you can set a keyboard shortcut for a bit of speed.
HTH, George
I had a similar problem and would be interested in a solution if there's a better one. What I ended up doing, though, was having each element's constructor find the document class (by calling this.parent until it found one that was of the right class) and then calling a function of that document class.
I was trying to give the the Game information about Locations which I placed through the IDE.
So I gave Location a constructor like this:
public function Location(){
var d:DisplayObject = parent;
while(!(d is Game)){
d = d.parent
}
d.addLocation(this);
}
Then in the Game class I was able to add them to an array and iterate through them. I'm positive this can't be the best way to do it, but it worked.
Basically, No... When you export to ActionScript in the library, it becomes a class. Same as other classes - MovieClip, Loader...etc. these are all compile-time properties. Basically, there is no way to find out these classes during run-time.
But, you mentioned iterating through MCs and doing something to them. If you need to do something with the MCs already on stage depending on their Class, You can do this using the following:
for (var i=0; i<this.numChildren; i++){
trace("Movie Name: "+this.getChildAt(i).name);
trace("Movie Class: "+getQualifiedClassName(this.getChildAt(i)));
}
Using the above loop and getQualifiedClassName may be an alternate solution.
精彩评论