使用JSFL批量管理flash

来源:互联网 发布:android 开发mac 教程 编辑:程序博客网 时间:2024/05/16 14:39

如果有这样一种需求: 美术提供了一组图片, 要把每个图片放入fla的资源库中,并对每个图片加一个导出链接名,并统一(或按照一个规则)设置在库中的图片的品质为70(默认为80),最后再对每一个fla导出swf.如果使用手工的方式依次创建fla并设置好再导出为swf, 是一件很麻烦的事情, 所以这个时候使用JSFL脚本进行批量处理是一件很方便的事情.

下面脚本是将34个球衣图片,批量生成fla,加上链接名,然后导出为swf.

//图片资源所在路径,要用file:///形式var rsPath="file:///D|/sns_football/UI/比赛场景/球衣/img/";//输出路径,要用file:///形式var outPath="file:///d|/sns_football/UI/比赛场景/球衣/";autoExec();//开始执行function autoExec(){ //清除当前控制台输出 fl.outputPanel.clear();  if(!parse()){  return; } //退出flash应用程序 //fl.quit();}function parse(){ //列出png文件 var fileList = FLfile.listFolder(rsPath+"*.png","files"); if(!fileList){  return false; } try{  var len=fileList.length;  for(var i=0; i<len; i++){   create(fileList[i]);   1/0;  } }catch(e){  log("",2,true,e);  return false;  }  return true;}/*生成fla,导入图片,到处swf@param pics 要导入一个fla中的所有图片*/function create(file){ //相当于在内存中创建一个fla. fl 可以理解为当前已经运行flash.exe程序 var dom=fl.createDocument(); //获取这个fla中的"库" var lib=dom.library; var id=0; var fileName=file.substring(0,file.lastIndexOf("."));  /* 导入一个文件到fla中. 第一个参数为文件路径. 第二个参数为: A Boolean value that specifies whether to import the file only into the document’s library (true)   or to also place a copy on the Stage (false).The default value is false.  (true表示导入到库中而不是放到舞台上) */ dom.importFile(rsPath+file,true); //用过"导入"的都知道,将文件导入到库中后,文件在库中的名称默认是文件本身的名称. 所以这里通过名称选择库中对应的那个资源进行属性设置. //lib.items 表示列出库中的所有资源 lib.selectItem(file); //如果导入的文件是图片(图片在库中是一个Bitmap,所以可以使用BitmapItem Object设置图片的属性. 参见API第53页) //设置是"允许平滑" lib.setItemProperty("allowSmoothing",false); /* Property; a string that determines the type of image compression applied to the bitmap. Acceptable values are "photo" or "lossless". If the value of bitmapItem.useImportedJPEGQuality is false, "photo" corresponds to JPEG with a quality from 0 to 100; if bitmapItem.useImportedJPEGQuality is true, "photo" corresponds to JPEG using the default document quality value. The value "lossless" corresponds to GIF or PNG format (see bitmapItem.useImportedJPEGQuality) 设置"压缩",使用"照片",而不是"无损". 如果要使用无损,填写"lossless". */ lib.setItemProperty("compressionType","photo"); lib.setItemProperty("quality",80);//品质  //以下是使用Item Object设置导出属性相关 //相当于选择"为运行时共享导入" lib.setItemProperty("linkageImportForRS",false); //相当于选择"为运行时共享导出" lib.setItemProperty("linkageExportForRS",false); //上面两项先进行设置,不然要错  /* Property; a Boolean value. If this property is true, the item is exported for ActionScript. You can also set the item.linkageExportForRS and item.linkageExportInFirstFrame properties to true. If you set this property to true, the item.linkageImportForRS property must be set to false. Also, you must specify an identifier (item.linkageIdentifier) and a URL(item.linkageURL). 很明显,设置是否"ActionScript导出",以便程序中通过链接名找到该资源 */ lib.setItemProperty("linkageExportForAS",true);  //相当于选择"在第1帧中导出" lib.setItemProperty("linkageExportInFirstFrame",true);  //设置AS链接名,相当于填写"类名" id=file.substring(0,file.length-5); lib.setItemProperty("linkageClassName","com.match.player."+id); //设置"基类"名.对图片进行as导出时,默认填充的类名就是"flash.display.BitmapData",所以不变的情况下不用设置 //lib.setItemProperty("linkageBaseClass","flash.display.BitmapData");  /* 开始生成swf. 第一个参数: A string, expressed as a file:/// URI, that specifies the name of the exported file.  If fileURI is empty or not specified, Flash displays the Export Movie dialog box.  This parameter is optional.输出路径(如果不填或者无效,则会弹出一个导出对话框,要求选择保存路径) 第二个参数: A Boolean value that, when set to true, causes Flash to use current SWF publish settings.  Otherwise, Flash displays the Export Flash Player dialog box.  The default is false. This parameter is optional. */ dom.exportSWF(outPath+fileName+".swf",true); log("导出"+outPath+fileName+".swf",1,true); //保存fla var flaURI=outPath+fileName+".fla"; if(FLfile.exists(flaURI)){//存在就删除旧的  FLfile.remove(flaURI); } fl.saveDocument(dom,flaURI); /* 关闭这个fla.  false表示在fla不会保存发生的改变并自动关闭.  true表示在fla发生改变时自动提示用户是否进行保存.  默认为true */ dom.close(false); log("产生"+fileName+".fla",1,true);}/*产生一个日志文件@param msg 信息@param type 1:普通消息 2:错误@param showConsole  是否要在控制台上打印*/function log(msg,type,showConsole,error){ type=type==null ? 1 : type; var output=new Date().toLocaleString()+"  "; if(type==2){  if(error){   var line=error.lineNumber ? error.lineNumber : (error.number ? error.number : -1);   output+="Error: \n"+(msg!="" ? msg+"\n" : "")+"ErrorType: "+error.name+(line!=-1 ? "\n第 "+line+" 行, " : "\n")+    error.message+(error.stack ? "\nStack Trace: "+error.stack : "");  } else {   output+="Error: "+msg;  }   } else {  output+=msg; } output+="\n"; /* 第一个参数: A string, expressed as a file:/// URI, specifying the file to which you want to write. 第二个参数: A string representing the text you want to place in the file. 第三个参数: An optional string with the value "append",  which specifies that you want to append textToWrite to the existing file.  If omitted, fileURI is overwritten with textToWrite.  如果不写"append",则表示覆盖写,不是追加写 */ FLfile.write(outPath+"log.txt",output,"append"); if(showConsole){  fl.trace(output); }} //=== end ======================================


原创粉丝点击