加载资产名为.swc到数组,在纯ActionScript 3项目 [英] Loading .swc assets into array, in pure Actionscript 3 project

查看:122
本文介绍了加载资产名为.swc到数组,在纯ActionScript 3项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道怎么弄的Flash CS4符号到Flash Builder通过名为.swc。 在类名成为主类可用,但我只能实例化的一个接一个,写每个名字进入code。

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.

如何通过我名为.swc循环,不提他们的名字载入其资产在一个数组中,然后获取和使用这些名称的实例?理想情况下,像(半称职的伪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.

推荐答案

您至少有2个选项。

选项1: 鉴于SWC文件是包含与嵌入式资产以及描述内容的XML文件中的SWF中的zip文件,你可以加载SWC为ZIP,获取XML并解析它。

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

我使用的是 nochump库

选项2: 因为你只需要类名,使您更容易一样,你提到的使用Flash CS4(这让我假定你有机会获得FLA文件生成SWC),你可以写会写该行$简单JSFL脚本C $下你。

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

所有你需要做的是: 文件>新建>闪存JavaScript文件并粘贴code。 使用描述性名称保存在Commands文件夹,如:listExportClasses。 因为它是在命令菜单中,如果你使用这个往往不够,你可以添加一个快捷键。

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.

该命令将是生成与FLA文件的名称的数组,并包含出口类的名称,方便地将其放置在剪贴板。

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.

例如

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

这篇关于加载资产名为.swc到数组,在纯ActionScript 3项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆