Sharing ActionScript code at runtime
Posted on Wednesday, January 12, 2006 @ 20:30 CET
Sharing images, other graphics, animations and such is handled easily in Flash by using Shared Libraries.
This way you could have one main Library.swf and its contents being used by other SWFs on a page. This is very useful if you have lots of objects being reused in several SWFs. First of all its easier to update (simply udpate the one Library.swf and republish, instead of updating all the elements in the different SWFs). Second the Library.swf would get cached by the web server too, cutting down on bandwidth and everyone is happy.
But what about code...
Now let me warn you, this is more of a thought process than anything.
Lets say you have lots (more than 10) of utility classes that have to be shared by lots of different Flash objects. When one of those classes changes, all of the SWFs that used that class have to be checked out from CVS (I'm assuming here ;), re-compiled and deployed. You have to keep track of where they are used, remember which ones you have re-compiled, which ones you have deployed... - a hassle.
What if you created a wrapper for these classes, cleverly called Wrapper. The purpose of this class would be simply to return instances of the different utility classes you require. For example:
import Dummy;import Vankerino;
class Wrapper {
public function Wrapper() {
// nothing
}
function getDummy():Dummy {
return new Dummy();
}
function getVankerino():Vankerino {
return new Vankerino();
}
}
Then you could create a SharedCode.fla and import the class (import Wrapper). Then you create a movie clip, export it for ActionScript and Runtime Sharing (call it "code"), and set the AS2 class to Wrapper. Now you have a FLA with a shared library, sharing one object - the movie clip with the Wrapper class thats ready to return instances of your utility classes.
Now each Flash object that requires these utility classes only needs to add one MovieClip to the Library and import for Runtime Sharing the code MovieClip in SharedCode.swf. Once that is done there are two ways to continue.
You could drag the imported MovieClip to the stage and give it an instance name of "codeLib". From then on, whenever you could simply do:
var dummy = codeLib.getDummy();dummy.callMyFunction();
The downside with this is that you have to drag it onto the stage and give it an instance name :)
The other way of doing it is:
var w = new Wrapper();d = w.getDummy();
dummy.callMyFunction();
This works and you would only have to instantiate all the classes as local objects.
Either way, you can't use type checking (dummy:Dummy) since the compiler doesn't know about the Dummy class before runtime. I think.
If you did all that, whenever the Dummy class got updated, all you would have to do is recompile SharedCode.fla, publish it and all the other SWFs that were using it would immediately get access to the new code in callMyFunction() without doing a thing.
Would this way of sharing code work in "the real world." I mean if you have enough shared code then it just feels right. Am I missing something here?
Anyways, the other way of streamlining this would be to have some kind of script-based build system that could recompile everything in one big swoop and deploy it as necessary. Opening each FLA by hand would just take too long.
They do say good programmers are lazy and dumb.
Lazy, because only lazy programmers will want to write the kind of tools that might replace them in the end. Lazy, because only a lazy programmer will avoid writing monotonous, repetitive code - thus avoiding redundancy, the enemy of software maintenance and flexible refactoring. Mostly, the tools and processes that come out of this endeavor fired by laziness will speed up the production.Then again, not all lazy and dumb programmers are good.
Note to self: Get something to display code properly
Update:
Turns out you must have an instance of the movie clip on the stage, but that's enough. It doesn't need an instance name - once its on the stage the code is available.
- paulo




Post a comment:
You must have Flash and JavaScript enabled to post a comment.