Okay, let me start by saying that I HATE importing resources into fla’s. When I first learned Flash [AS2] it wasn’t an issue. Now with AS3 and the necessity of classes my coding style has changed and I really, really like keeping things as separated into classes as possible.
The Problem With MovieClips
When attempting to program button classes I ran into this wall: Flash’s MovieClip is the only appropriate class object to use for animation. It’s great for placing images in your timeline to form moving pictures and placing Actionscript on certain frames makes directing the clip around really easy. But what about when you have to make MovieClips dynamically and add code to certain frames inside that clip, without using the GUI? MovieClip doesn’t support dynamically adding frames, or functions or Bitmaps to certain frames. This sucks. Why Adobe did this I do not know – so I wrote a class that DOES WHAT I WANT [and maybe you too].
Animation takes three parameters, which are all optional: numFrames:int = 1 width:int = 0, and height:int = 0. These params just initiate the object.
* blog.efnx.com:Animation Example
import com.efnx.utils.Animation; //import the class
var character:Animation = new Animation(); //object sets numFrames, width and height to 1, 0 and 0
After making the object you have to append the pictures to certain frames using the appendBitmapData function:
What this does is take the BitmapData and attach a function to the given frame that replaces the Animation’s default BitmapData with yours upon execution of that frame.
Doing this achieves the same effect as dragging a bitmap onto the stage in a MovieClip keyframe.
I wrote the class keeping in mind that someone may want to use it not for animation but sequential code execution, so frames can be created and functions attached to each frame without any graphics associated. The function used to add [or append] other functions to a certain frame is
appendFrameScript(theFrame:int, theFunction:Function, [testing:Boolean = false]);
theFrame is the frame you’d like to add a function to, theFunction is the function to be added and testing tells the class whether or not to print debug info.
After adding frames and bitmaps and all that Animation can be controlled using familiar MovieClip methods like gotoAndPlay(), gotoAndStop(), go(), stop() and properties like currentFrame and totalFrames, as well as some others – bmd [the current frames BitmapData], bmdArray [an array of all frames BitmapData] and cool functions like customFps(fps:int) which set a custom playback rate denoted in number of frames per second [fps:int] and accelerateFps(desiredFps:int, numberFrames:int) which accelerates the playback delay from the current FPS to the desired FPS in numberFrames frames. accelerateFps is still a little buggy, feel free to nice it up!
Animation doesn’t automatically loop your animation [since I usually don't need it to], so to really mimic a MovieClip you’d have to write a function incorporating gotoAndPlay(1) and then attach the function to the frame you’d like to loop your clip at:
var character:Animation = new Animation(); //initiate object
character.appendBitmapData(1, firstBitmapData); //add BitmapData
character.appendFrameScript(2, repeat); //append repeat function
function repeat():void //repeat function
Hope this class helps some of you out. Feel free to let me know what needs improving and comment if you like it!
Animation Class Source Code