Adobe, MAKE SOME NOISE

SWF: Automatic swf loader

September 15th, 2008

Download this class

SWF: [AS3] [Zip] [doc]

When I posted the Pic class earlier, I knew immediately that I should code a companion class to handle the specific loading of swf files. With static images, you usually don't need a handle on the actual bitmap, but when you get into loading external Flash content, you generally want to interact with it.

To that end, I'm working on a class that will pass all the normal MovieClip properties back and forth between the parent and the loaded swf as though the wrapper were the swf itself, but that's going to take a while to really put together and test.

NOTE: I briefly considered a monkey-patch to just add a little loader directly onto the native MovieClip class but the idea of modifying all MC instances in the entire language just to add one niggling bit of functionality makes my blood boil. If Adobe wanted us to ignore the Loader class, they would have built things that way to start with (and named the whole mess AS2)!

In the meantime, here's the SWF class; A lightweight (~1500 bytes) loader that hides all the nitty-gritty stuff so you can just drop it in and go. Loading a swf and putting it on stage is as easy as this:

  1. import com.calypso88.SWF;
  2.  
  3. var mySWF:SWF = new SWF('http://www.calypso88.com/test.swf');
  4. addChild(mySWF);

Fine and dandy - but you'll want to get into the guts of the loaded content. To do that you need to wait for the loading to finish and then target the .mc property. Here's how:

  1. import com.calypso88.SWF;
  2.  
  3. var mySWF:SWF = new SWF('http://calypso88.com/tester.swf', false);
  4. addChild(mySWF);
  5.  
  6. // trace out the loading %
  7. mySWF.addEventListener(ProgressEvent.PROGRESS, onProg);
  8.  
  9. function onProg(e:ProgressEvent):void{
  10. trace(e.target.percent);
  11. }
  12.  
  13. // when loaded, customize the guts
  14. mySWF.addEventListener(Event.COMPLETE, onLoaded);
  15.  
  16. function onLoaded(e:Event):void{
  17. trace('complete');
  18. mySWF.mc.spinner.stop();
  19. mySWF.mc.txt.text = 'Hello from the outside!';
  20. mySWF.mc.gotoAndStop(2);
  21. }
  22.  
  23. // start loading
  24. mySWF.go();

To stay consistent with the Pic class, I included a clone function here too, but again, the way the loaders work, this requires some trickery so you can't just clone the swf and start manipulating, you'll have to wait for it to get initialized:

  1. import com.calypso88.SWF;
  2.  
  3. var mySWF:SWF = new SWF('http://calypso88.com/tester.swf', false);
  4.  
  5. mySWF.addEventListener(SWF.LOADED, onLoaded);
  6.  
  7. function onLoaded(e:Event):void{
  8. addChild(mySWF);
  9. var myOtherSWF:MovieClip = mySWF.cloneMC(false);
  10. myOtherSWF.y = 10 + mySWF.height;
  11. addChild(myOtherSWF);
  12.  
  13. myOtherSWF.addEventListener(SWF.CLONE_COMPLETE, function(e:Event):void{
  14. // the clone uses the same .mc wrapper as the original
  15. myOtherSWF.mc.txt.text = 'This swf is a clone!';
  16. e.target.mc.spinner.stop();
  17. });
  18. }
  19.  
  20. mySWF.go();


SECURITY WARNING: This class uses the URLLoader class instead of the more traditional Loader class. For most users this won't be a problem but if you are building an application that allows the loading of swfs from outside users, this class will open your swf to the possibility of cross-site script-injection. Use this at your own risk!

And just because this post isn't long enough, here are a few shots out of the Documentation Cannon!

Pic: Automatic image loader

August 15th, 2008

Download this class

Pic: [AS3] [Zip] [doc]

Updated: Version 1.1.0

Here's a little throwaway class that I've been using for a while. For a skimpy 1200 1500 bytes, this little guy handles image loading and smoothing, and it will even spin off duplicate Bitmaps for thumbnails. w00t!

Simple implementation:

  1. import com.calypso88.Pic;
  2.  
  3. var p:Pic = new Pic('http://make-some-noise.info/badges/88x31.gif');
  4. addChild(p);

And some advanced features:

  1. import com.calypso88.Pic;
  2.  
  3. var p:Pic = new Pic('http://make-some-noise.info/badges/88x31.gif', true, false);
  4. addChild(p);
  5. p.x = p.y = 40;
  6. p.rotation = 10;
  7.  
  8. p.addEventListener(Pic.LOADED, function(e:Event):void{
  9. // create thumbnail
  10. var thumb:Bitmap = p.cloneBitmap(false);
  11. thumb.scaleX = thumb.scaleY = .5;
  12. thumb.rotation = 10;
  13. addChild(thumb);
  14.  
  15. // change the original
  16. p.bitmapData.floodFill(10, 10, 0xFF6600);
  17. p.bitmap.smoothing = false;
  18. });
  19.  
  20. p.go();

And of course – it's Free-Documentation-Friday!

UPDATE: I've posted a companion class for loading SWF content. It's not quite as seamless as I want yet, but it's still a lot faster and easier than writing out loaders all day. Check it out.