Skip to main content
April 24, 2009
Question

Dynamically Load Bitmaps from SWC

  • April 24, 2009
  • 1 reply
  • 2065 views

Working with AS3 and CS4:

I would like to have a SWC file with many bitmaps stored in it, which can the be called and loaded dynamically when they are needed.

The SWC has been made, with the Bitmaps set to export.

In my main class I am running into trouble trying to access these classes.  Different sources are offering different advice, could someone advise the best way to gain access to these classes within the SWC, and the best way to access the SWC from within my file.

********

Note: from google found others describing the same problem:

http://www.flashdevelop.org/community/viewtopic.php?f=13&t=4327

they offer a solution which involves having to statically name the classes at runtime (these arent exported bitmapdata classes but same idea)

http://synja.com/?p=29

this second link is a person describing a solution to the problem described in the first link.  His solution involves what appear to be either flex or command line compiler directives.

 

The include commands he uses dont seem to be valid in cs4.  Is there an equivalent that will work, the literature I have read seems to suggest it can all be done with the externalLibrary, or Library location selecting.  I have place my swc in both with no success.

This topic has been closed for replies.

1 reply

April 24, 2009

I found a similar solution described on these forums from a while back:

http://forums.adobe.com/message/241473#241473

Again, its the flex based compiler includes.  Can anyone tell me the equivalent in cs4, as just including them in the project library paths does NOT work.

April 24, 2009

I looked into dynamically loading SWC libs awhile back, and I came up with this. While it functions, it is by no means complete. But you should be able to gleen something from it.


// requires AS3 zip library from nochump.com
//
http://www.nochump.com/blog/?p=15

import flash.utils.IDataInput;
import nochump.util.zip.*;


var swcPath:String = "MyImageLibrary.swc";

// we need a Loader instance to write the uncompressed library bytes to
var libLoader:Loader = new Loader();
libLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, libLoadCompleteHandler);

// load the swc
var urlLoader:URLLoader = new URLLoader()
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;

var request:URLRequest= new URLRequest(swcPath);
urlLoader.addEventListener(Event.COMPLETE, swcLoadCompleteHandler);
urlLoader.load(request);

function swcLoadCompleteHandler(event:Event)
{
var loadedData:IDataInput = event.target.data as IDataInput;

var zipFile:ZipFile = new ZipFile(loadedData);

// this example assumes there is only one swf file
// in the swc :  library.swf
// would be safer to parse catalog.xml
for(var i:int = 0; i < zipFile.entries.length; i++)
{    
  var entry:ZipEntry = zipFile.entries;    
  var data:ByteArray = zipFile.getInput(entry);  
 
  if(entry.name =="catalog.xml")
  {
   // we'll do nothing in this example
  }
  if(entry.name =="library.swf")
  {
   // load the library
   libLoader.loadBytes(data);
  }
}
}

function libLoadCompleteHandler(event:Event):void
{
var dom:ApplicationDomain = libLoader.contentLoaderInfo.applicationDomain;
try
{
  var bmpClass:Class = dom.getDefinition("Image000") as Class;
  if(bmpClass != null)
  {
   var bmp:Bitmap = new Bitmap();
  
   // need to pass 2 arguments to a BitmapData ctor
   bmp.bitmapData = new bmpClass(1,1) as BitmapData
   this.addChild(bmp);
  }
 
}
catch(e)
{
  trace(e);
}
}