Loading .swc assets into an array in an ActionScript 3 project

I know how to get Flash CS4 characters in Flash Builder via .swc. Class names become available in the main class, but I can only create instances one by one, writing each name in the code.

How can I go through .swc and load its assets into an array without mentioning their name, and then get and use these names to instantiate? ideally, something like (semi-axis pseudo-code):

the_instances: = new Array for(i=0; i<the_SWC.length; i++) { tmp = new eval( the_SWC[i].name + '\(\)' ) the_instances.push( tmp ) } 

or anything else to get these names in a loop.

+2
import symbols class actionscript-3 swc
source share
1 answer

You have at least 2 options.

Option1: Given that the SWC file is a zip file containing a swf with embedded assets and an xml file describing the contents, you can load swc as a zip, get the xml and parse it.

 var swcLoader:URLLoader = new URLLoader(new URLRequest('assets/assetsLib.swc')); swcLoader.dataFormat = URLLoaderDataFormat.BINARY; swcLoader.addEventListener(Event.COMPLETE, swcLoaded); function swcLoaded(event:Event):void{ var zipFile:ZipFile = new ZipFile(event.target.data); for(var i:int = 0; i < zipFile.entries.length; i++) { var entry:ZipEntry = zipFile.entries[i]; if(entry.name == 'catalog.xml'){ var data:ByteArray = zipFile.getInput(entry); var list:XML = new XML(zipFile.getInput(entry)); var nodes:XMLList = list.children(); for (var j:int = 0; j < nodes.length(); j++) { if (nodes[j].name().localName == "libraries") { var libraries:XML = nodes[j]; var libList:XMLList = libraries.children(); for(var k:int = 0 ; k < libList.length(); k++){ var library:XML = libList[k]; var classList:XMLList = library.children(); for(var l:int = 0 ; l < classList.length(); l++){ var classDef:XML = classList[l]; trace('class name: ' + classDef.@name); //var LibClass:Class = this.loaderInfo.applicationDomain.getDefinition(classDef.@name) as Class; } } } } } } } 

I am using nochump library .

Option2: Since you only need class names to simplify them, and you mentioned using Flash CS4 (which makes me assume that you have access to the .fla file generating swc), you can write a simple jsfl script that will write this line of code for you.

 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); 

All you have to do is: File> New> Flash Javascript File and paste the code. Save it in the Commands folder with a descriptive name, for example: listExportClasses. Since this is on the Commands menu, if you use it often enough, you can add a keyboard shortcut.

That the command will generate an array with the file name fla and contain the names of the exported classes and conveniently put it on the clipboard.

eg

 var assetsLibClasses = ["Start1","Start2","Start3","Button","ColorBox","GameBackground","MenuBackground"]; 
+2
source share

All Articles