English 中文(简体)
Loading .swc assets into array, in pure Actionscript 3 project
原标题:

I know how to get Flash CS4 symbols into Flash Builder via .swc. The class names become available in the main class, but I can only instantiate those one by one, writing each name into the code.

How can I loop through the .swc and load its assets in an array without mentioning their name, then obtain and use these names for instantiation? ideally, something like (half-assed pseudocode):

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 those names in a loop.

问题回答

You have at least 2 options.

Option1: Given the fact that a SWC file is a zip file that contains a swf with the embedded assets and an xml file describing the contents, you can load the 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 the nochump library.

Option2: Since you only need the class names to make your like easier and you mentioned using Flash CS4(which makes me assume you have access to the .fla file generating the swc), you can write a simple jsfl script that will write that 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 need to do is: File > New > Flash Javascript File and paste the code. Save it in the Commands folder with a descriptive name, like: listExportClasses. Since it s in the Commands menu, if you use this often enough you could add a keyboard shortcut.

What the command will is generate an array with the name of the fla file and contain the exported classes names and conveniently place it in your clipboard.

e.g

var assetsLibClasses = ["Start1","Start2","Start3","Button","ColorBox","GameBackground","MenuBackground"];




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签