AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Actionscript Code Framework for a Flash Movie


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?

A framework for procedural code in one frame of the 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  *****************/

/***  1. DOCUMENT MOVIE, AUTHOR, HISTORY  ***/

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
*/

/***  2. IMPORT CLASSES  ***/

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;

/***  3. DECLARE AND INITIALIZE VARIABLES  ***/

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);

/***  4. DEFINE FUNCTIONS, INCLUDING INIT FOR MOVIE SETUP  ***/

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.

/***  5. CALL THE INIT FUNCTION TO START THE MOVIE  ***/

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();

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads