In preceding pages of this section, we've looked at some of the building blocks of a Flash movie/application: variables, code blocks like if/else and for, event handlers (onRelease, onEnterFrame, eg) and functions. But how do these things fit together into a movie?
Below is an outline of sections of code which can be cut and pasted into a frame of a new movie to give a guideline for setting up that movie. Not all sections are required for all movies, but by looking at this, you can at least get a suggestion for starting to organize your code. Remember that any code you put into a frame can only access movieclips and textfields which exist in that frame, so if possible, structure your movie so that all assets exist in one main frame. A common scenario is to reserve a few frames at the beginning of the movie for a preloader and put all your code and movie assets into say, frame 10. For an example of the implementation of this framework, see the code for the TV player on the toggle buttons page.
Code Framework for a procedurally-based movie
/********* 1. DOCUMENT MOVIE, AUTHOR, HISTORY ***************************/ /********* 2. IMPORT CLASSES ********************************************/ /********* 3. DECLARE AND INITIALIZE VARIABLES **************************/ /********* 4. DEFINE FUNCTIONS, INCLUDING INIT FOR MOVIE SETUP **********/ /********* 5. CALL THE INIT FUNCTION TO START THE MOVIE *****************/
It's a good idea for your future use, and that of anyone else looking at your movie, to start your movie with a documentation area, in which you state the name and purpose of the movie, the author(s) and any attributions you wish to make, and the version history (so you can see what changes you made when, and why). An example (only an example; there are many and probably better documenting schemes) is:
/*
Movie: tv_movieplayer.fla
Description: a framework fla for loading and viewing external swfs.
Author: Helen Triolo, for use with
http://flash-creations.com/notes/actionscript_togglebutton.php
History:
v 1.01 2006/02/21 Modified the framework per description on this page:
http://flash-creations.com/notes/actionscript_codestructure.php
v 1.00 2006/02/01 Includes sound on/off, rewind, play/pause controls,
ability to load one movie by clicking on the channel 1 button
*/
There are occasions where you will need to import classes in order for your movie to run, either Flash's own classes (ones which are not included in the IDE by default) like the Tween class or the Delegate class, or other custom classes, like Alex Uhlmann's Animation Package, eg. This is the place to do that. An example of such an import statement is:
import mx.utils.Delegate;
Any variable that you want to be available throughout the movie should be declared here. If it has an initial value (such as an isPlaying variable in a movie where the item being tracked is paused on startup), then the variable may also be assigned an initial value here. Alternately, that may be done in the init function or elsewhere instead. Here's an example of a declaration and a declaration/assignment:
// declare a variable counter but don't give it a value yet var counter:Number; // declare an instance of the Sound class, so, and associate it with the main timeline var so:Sound = new Sound(this);
This section is usually the heart of your code, where you set aside blocks of code that will perform specific actions, and give those blocks names. As described in the Function section of the Code Blocks page, functions may be defined to handle certain events (like the mouse being clicked on a control movieclip), or to carry out certain calculations, like the randomBetween function. Here's a quick example in which an event handler is defined, and an init function is defined which sets a variable counter to 0, hides photoholder_mc, and assigns the enlarge function to the onRollOver property of openpic_mc:
function enlarge() {
this._xscale = 200;
this._yscale = 200;
}
function init() {
counter = 0;
photoholder_mc._visible = false;
openpic_mc.onRollOver = enlarge;
}
One specific function that is good to include in this section is an init function, which will contain all the initialization that needs to be done to elements on the stage, sounds and assignment of handler functions to event properties. Setting it aside as a separate function allows you to call it from wherever you need to, either as the last thing you do in the code on the current frame, or from another movie that's loading this one.
If you've included all of your startup assignments in the init function, the only code you need in this section is a call to init. If you're loading this movie from another movie, you won't need that init call here; instead it will be done by the loader movie, as in the example on the control sound page. To start the code in this page, this is all you need in this section:
init();
last update: 21 Feb 2006
Discussed on this page:
an example framework for putting code in main frame of movie: set up areas to document movie, import classes, declare and/or initialize variables, define functions, start movie