How not to hate SWF libraries

I have been playing with JSFL quite a bit recently and this little known technique may save you a a lot of time while working with SWF libraries. One thing I hate about Flash is linking library assets to classes. Actually I think linking library assets to already existing Classes is not a very good programming technique. Instead I always make sure they are anonymous classes generated at compile time.

Anyway I am getting side tracked. The process in either case to do this is really, really, really tedious and on a few occasions now I have been whinging to the guys at Adobe about this and how hopeless it is. But after many years it's still just the same and lame.

You might ask why not use SWCs to manage library assets? Problem I have with SWCs is if you use them for all the assets in your project then your compile times will eventually go through the roof! This is something which your team could waste days or even weeks of time especially in longer production phases.

This said I still want a process where I can load assets from a SWF library with some kind of strict type safety and have not just rely on resolving library assets at runtime. This could allow for you to be aware that a library asset is missing before compiling.

Creating a bulletproof utility to map existing classes to your library assets would be quite difficult to setup. So instead I have written something simple (initially) which allows for you to automatically set library assets to be exported with linkage ids. This works by basically using the library name as the linkage id for an asset and iterating through selected library assets.

var packageName=prompt("Library package name","com.library.display");

 

var selItems = fl.getDocumentDOM().library.getSelectedItems(); 

 

for(var i =0; i<selItems.length; i++){

	item = selItems[i];

 

	var className=item.name;

 

	var sliceIndex=className.lastIndexOf("/");

	className.slice(sliceIndex+1,className.length);

 

	className = className.split(" ").join("");

 

	var dotIndex=className.lastIndexOf(".");

	if(dotIndex!=-1) className=className.slice(0,dotIndex);

 

	item.linkageExportForAS = true;

	item.linkageExportInFirstFrame = true;

	item.linkageClassName = packageName+"."+className;

 

	fl.outputPanel.trace(item.linkageClassName);

}

 

alert("Exported complete!");

Although this saves quite a bit of time it's still kinda annoying, I would have to copy and paste linkage ids from the woeful Flash IDE library and I would not get any intellisense from FlashBuilder or FDT.

Although we can take this same idea a bit further to accommodate better support for intellisense with library assets from a SWF library with FlashBuilder or FDT. Although it's important and maybe obvious this technique will require loading the SWF library assets into the parent applicationDomain by just using a Loader instance. Next by using JSFL you can create classes automatically based on the library items type. Here I am probably achieving this by an unintended use of the save function from the outputPanel but it allows for you to write classes and then save them into a particular folder.

fl.outputPanel.clear();

fl.outputPanel.trace(templateClass);

fl.outputPanel.save(saveLocation+folderPath+"LibraryLinkageReferences.as");

 

I only set this JSFL up for Buttons, MovieClips and Bitmaps but this could be extended to work with other types. The JSFL to do this looks like :


var saveLocation=fl.browseForFolderURL("Save classes location");

var packageName=prompt("Package name","com.library.display");

var folderPath = packageName.split(".").join("/")+"/";

 

FLfile.createFolder(saveLocation+folderPath);

 

var selItems=fl.getDocumentDOM().library.getSelectedItems();

 

for (var i =0; i< selItems.length; i++) {

	item= selItems[i];

 

	var className=item.name;

 

	var sliceIndex=className.lastIndexOf("/");

	className.slice(sliceIndex+1,className.length);

 

	className = className.split(" ").join("");

 

	var dotIndex=className.lastIndexOf(".");

	if(dotIndex!=-1) className=className.slice(0,dotIndex);

 

	item.linkageExportForAS = true;

	item.linkageExportInFirstFrame = true;

	item.linkageClassName = packageName+"."+className;

 

	var templateClass='package '+packageName+' {

		classImports

		public class className extends baseClassName {

			public function className() {

				classConstructor

			}

		}

	}';

 

	var linkageName=item.linkageClassName;



	var className=item.name;



	var sliceIndex=className.lastIndexOf("/");

	className.slice(sliceIndex+1,className.length);

 

	className = className.split(" ").join("");

 

	var dotIndex=className.lastIndexOf(".");

	if(dotIndex!=-1) className=className.slice(0,dotIndex);

 

	className = className.substr(0, 1).toUpperCase()+className.substr(1, className.length-1);

 

	var baseClassName=item.linkageBaseClass;

	var classConstructor='addChild(new (getDefinitionByName("linkageName"))());';

	var classImports = '	import flash.utils.getDefinitionByName;\n';

 

	switch (item.itemType) {

		case "bitmap" :

			baseClassName="flash.display.Bitmap";

			classConstructor='super(new (getDefinitionByName("linkageName"))(0, 0));';

		break;

		case "movieclip" :

			baseClassName="flash.display.MovieClip";

		break;

		case "button" :

			baseClassName="flash.display.SimpleButton";

		break;

	}

 

	classImports+='	import '+baseClassName+';';

 

	templateClass=templateClass.replace(/className/g,className);

	templateClass=templateClass.replace(/classImports/g,classImports);

	templateClass=templateClass.replace(/classConstructor/g,classConstructor);

	templateClass=templateClass.replace(/baseClassName/g,baseClassName);

	templateClass=templateClass.replace(/linkageName/g,linkageName);

 

	fl.outputPanel.clear();

	fl.outputPanel.trace(templateClass);

	fl.outputPanel.save(saveLocation+folderPath+className+".as");

	fl.outputPanel.clear();

}

 

Though if you don't like using this approach of creating stub classes for retrieving library assets from a SWF library then there is one last technique using JSFL which you can use whilst still having some ability to use intellisense. Like I mentioned before the most annoying part of working with SWF library assets is not having intellisense. We could then instead of creating classes as a reference to the library SWF class, automatically create a single LibraryLinkageReferences class which contains static constants which are references to the library linkage ids.

var saveLocation=fl.browseForFolderURL("Save class location");

var libraryPackageName=prompt("Library package name","library");

var classPackageName=prompt("Class package name","com.library.display");

 

var folderPath = classPackageName.split(".").join("/")+"/";

var templateClass = 'package '+classPackageName+' {\n	public class LibraryLinkageReference{\n';

 

FLfile.createFolder(saveLocation+folderPath);

 

var selItems =fl.getDocumentDOM().library.getSelectedItems();

 

for (var i =0; i< selItems.length; i++) {

	item= selItems[i];

 

	var className=item.name;

 

	var sliceIndex=className.lastIndexOf("/");

	className.slice(sliceIndex+1,className.length);

 

	className = className.split(" ").join("");

 

	var dotIndex=className.lastIndexOf(".");

	if(dotIndex!=-1) className=className.slice(0,dotIndex);

 

	className = className.substr(0, 1).toUpperCase()+className.substr(1, className.length-1);

 

	item.linkageExportForAS = true;

	item.linkageExportInFirstFrame = true;

	item.linkageClassName = libraryPackageName+"."+className;

 

	templateClass+="public static const "+className.toUpperCase()+":String = \""+item.linkageClassName+"\";\n";

}

 

templateClass+='	}\n}';

 

fl.outputPanel.clear();

fl.outputPanel.trace(templateClass);

fl.outputPanel.save(saveLocation+folderPath+"LibraryLinkageReferences.as");

 

alert("Exported complete saved to : "+saveLocation+folderPath+"LibraryLinkageReferences.as");

Hopefully this is some food for thought or very useful. I know I find these techniques and JSFL scripts very handy :)

link:http://www.lostinactionscript.com/blog/index.php/2010/03/06/how-not-to-hate-swf-libraries/

var saveLocation=fl.browseForFolderURL("Save classes location");
var packageName=prompt("Package name","com.library.display");
var folderPath = packageName.split(".").join("/")+"/";
 
FLfile.createFolder(saveLocation+folderPath);
 
var selItems=fl.getDocumentDOM().library.getSelectedItems();
 
for (var i =0; i< selItems.length; i++) {
item= selItems[i];
 
var className=item.name;
 
var sliceIndex=className.lastIndexOf("/");
className.slice(sliceIndex+1,className.length);
 
className = className.split(" ").join("");
 
var dotIndex=className.lastIndexOf(".");
if(dotIndex!=-1) className=className.slice(0,dotIndex);
 
item.linkageExportForAS = true;
item.linkageExportInFirstFrame = true;
item.linkageClassName = packageName+"."+className;
 
var templateClass='package '+packageName+' {
classImports
public class className extends baseClassName {
public function className() {
classConstructor
}
}
}'
;
 
var linkageName=item.linkageClassName;
 
var className=item.name;
 
var sliceIndex=className.lastIndexOf("/");
className.slice(sliceIndex+1,className.length);
 
className = className.split(" ").join("");
 
var dotIndex=className.lastIndexOf(".");
if(dotIndex!=-1) className=className.slice(0,dotIndex);
 
className = className.substr(0, 1).toUpperCase()+className.substr(1, className.length-1);
 
var baseClassName=item.linkageBaseClass;
var classConstructor='addChild(new (getDefinitionByName("linkageName"))());';
var classImports = ' import flash.utils.getDefinitionByName;\n';
 
switch (item.itemType) {
case "bitmap" :
baseClassName="flash.display.Bitmap";
classConstructor='super(new (getDefinitionByName("linkageName"))(0, 0));';
break;
case "movieclip" :
baseClassName="flash.display.MovieClip";
break;
case "button" :
baseClassName="flash.display.SimpleButton";
break;
}
 
classImports+=' import '+baseClassName+';';
 
templateClass=templateClass.replace(/className/g,className);
templateClass=templateClass.replace(/classImports/g,classImports);
templateClass=templateClass.replace(/classConstructor/g,classConstructor);
templateClass=templateClass.replace(/baseClassName/g,baseClassName);
templateClass=templateClass.replace(/linkageName/g,linkageName);
 
fl.outputPanel.clear();
fl.outputPanel.trace(templateClass);
fl.outputPanel.save(saveLocation+folderPath+className+".as");
fl.outputPanel.clear();
}

你可能感兴趣的:(swf)