SWF: Automatic swf loader
September 15th, 2008
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:
import com.calypso88.SWF; var mySWF:SWF = new SWF('http://www.calypso88.com/test.swf'); 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:
import com.calypso88.SWF; var mySWF:SWF = new SWF('http://calypso88.com/tester.swf', false); addChild(mySWF); // trace out the loading % mySWF.addEventListener(ProgressEvent.PROGRESS, onProg); function onProg(e:ProgressEvent):void{ trace(e.target.percent); } // when loaded, customize the guts mySWF.addEventListener(Event.COMPLETE, onLoaded); function onLoaded(e:Event):void{ trace('complete'); mySWF.mc.spinner.stop(); mySWF.mc.txt.text = 'Hello from the outside!'; mySWF.mc.gotoAndStop(2); } // start loading 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:
import com.calypso88.SWF; var mySWF:SWF = new SWF('http://calypso88.com/tester.swf', false); mySWF.addEventListener(SWF.LOADED, onLoaded); function onLoaded(e:Event):void{ addChild(mySWF); var myOtherSWF:MovieClip = mySWF.cloneMC(false); myOtherSWF.y = 10 + mySWF.height; addChild(myOtherSWF); myOtherSWF.addEventListener(SWF.CLONE_COMPLETE, function(e:Event):void{ // the clone uses the same .mc wrapper as the original myOtherSWF.mc.txt.text = 'This swf is a clone!'; e.target.mc.spinner.stop(); }); } 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!
Eureka!
September 4th, 2008
This stunning video turned up in my news feeds today and it got me thinking:
Lightning, you cheeky bastard! Is that naturally occurring pathfinding or are you just happy to see me!?
As soon as I saw the beginning of that video I was reminded of my first maze test, and the unintended (yet quite interesting to watch) motion as the algorithm chewed through the possible paths. What's more, my latest maze experiment was built around the concept of tracing the path-of-least-resistance through a bitmap – precisely how the lightning is searching through the ionosphere for a positive electrical ground!
It took me about 5 minutes to overwrite the old maze with some render clouds and throw a little glow filter at the final path - and the result already looks pretty good. This is definitely a rough pass (there's a pixel-trap somewhere that's hanging the algorithm so I just jammed it all into a try/catch and ignored the timeout error), and there is a lot of weird clumping that I need to iron out, but the core concept is there.
Keep an eye out – I'll try and put up an animated (and less buggy) version sometime this weekend.

