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.

  1. var packageName=prompt”Library package name”,”com.library.display”;
  2. var selItems = fl.getDocumentDOM.library.getSelectedItems;
  3. forvar i =0; i<selItems.length; i++
  4. item = selItemsi;
  5. var;
  6. var sliceIndex=className.lastIndexOf”/”;
  7. className.slicesliceIndex+1,className.length;
  8. className = className.split” “.join””;
  9. var dotIndex=className.lastIndexOf”.”;
  10. ifdotIndex!=-1 className=className.slice0,dotIndex;
  11. item.linkageExportForAS = true;
  12. item.linkageExportInFirstFrame = true;
  13. item.linkageClassName = packageName+”.”+className;
  14. fl.outputPanel.traceitem.linkageClassName;
  15. 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.

  1. fl.outputPanel.clear;
  2. fl.outputPanel.tracetemplateClass;
  3. fl.outputPanel.savesaveLocation+folderPath+””;

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 :

  1. var saveLocation=fl.browseForFolderURL”Save classes location”;
  2. var packageName=prompt”Package name”,”com.library.display”;
  3. var folderPath = packageName.split”.”.join”/”+”/”;
  4. FLfile.createFoldersaveLocation+folderPath;
  5. var selItems=fl.getDocumentDOM.library.getSelectedItems;
  6. for var i =0; i< selItems.length; i++
  7. item= selItemsi;
  8. var;
  9. var sliceIndex=className.lastIndexOf”/”;
  10. className.slicesliceIndex+1,className.length;
  11. className = className.split” “.join””;
  12. var dotIndex=className.lastIndexOf”.”;
  13. ifdotIndex!=-1 className=className.slice0,dotIndex;
  14. item.linkageExportForAS = true;
  15. item.linkageExportInFirstFrame = true;
  16. item.linkageClassName = packageName+”.”+className;
  17. var templateClass=’package ‘+packageName+’ {
  18. classImports
  19. public class className extends baseClassName {
  20. public function className() {
  21. classConstructor
  22. }
  23. }
  24. }’;
  25. var linkageName=item.linkageClassName;
  26. var;
  27. var sliceIndex=className.lastIndexOf”/”;
  28. className.slicesliceIndex+1,className.length;
  29. className = className.split” “.join””;
  30. var dotIndex=className.lastIndexOf”.”;
  31. ifdotIndex!=-1 className=className.slice0,dotIndex;
  32. className = className.substr0, 1.toUpperCase+className.substr1, className.length-1;
  33. var baseClassName=item.linkageBaseClass;
  34. var classConstructor=’addChild(new (getDefinitionByName(“linkageName”))());’;
  35. var classImports = ‘ import flash.utils.getDefinitionByName;\n’;
  36. switch item.itemType
  37. case “bitmap” :
  38. baseClassName=”flash.display.Bitmap”;
  39. classConstructor=’super(new (getDefinitionByName(“linkageName”))(0, 0));’;
  40. break;
  41. case “movieclip” :
  42. baseClassName=”flash.display.MovieClip”;
  43. break;
  44. case “button” :
  45. baseClassName=”flash.display.SimpleButton”;
  46. break;
  47. classImports+=’ import ‘+baseClassName+’;’;
  48. templateClass=templateClass.replace/className/g,className;
  49. templateClass=templateClass.replace/classImports/g,classImports;
  50. templateClass=templateClass.replace/classConstructor/g,classConstructor;
  51. templateClass=templateClass.replace/baseClassName/g,baseClassName;
  52. templateClass=templateClass.replace/linkageName/g,linkageName;
  53. fl.outputPanel.clear;
  54. fl.outputPanel.tracetemplateClass;
  55. fl.outputPanel.savesaveLocation+folderPath+className+”.as”;
  56. 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.

  1. var saveLocation=fl.browseForFolderURL”Save class location”;
  2. var libraryPackageName=prompt”Library package name”,”library”;
  3. var classPackageName=prompt”Class package name”,”com.library.display”;
  4. var folderPath = classPackageName.split”.”.join”/”+”/”;
  5. var templateClass = ‘package ‘+classPackageName+’ {\n public class LibraryLinkageReference{\n’;
  6. FLfile.createFoldersaveLocation+folderPath;
  7. var selItems =fl.getDocumentDOM.library.getSelectedItems;
  8. for var i =0; i< selItems.length; i++
  9. item= selItemsi;
  10. var;
  11. var sliceIndex=className.lastIndexOf”/”;
  12. className.slicesliceIndex+1,className.length;
  13. className = className.split” “.join””;
  14. var dotIndex=className.lastIndexOf”.”;
  15. ifdotIndex!=-1 className=className.slice0,dotIndex;
  16. className = className.substr0, 1.toUpperCase+className.substr1, className.length-1;
  17. item.linkageExportForAS = true;
  18. item.linkageExportInFirstFrame = true;
  19. item.linkageClassName = libraryPackageName+”.”+className;
  20. templateClass+=”public static const “+className.toUpperCase+”:String = \””+item.linkageClassName+”\”;\n”;
  21. templateClass+=’ }\n}’;
  22. fl.outputPanel.clear;
  23. fl.outputPanel.tracetemplateClass;
  24. fl.outputPanel.savesaveLocation+folderPath+””;
  25. alert”Exported complete saved to : “+saveLocation+folderPath+””;