Browse DevX
Sign up for e-mail newsletters from DevX


Organize Your ActionScript:Six Strategies for Healthier Flash Code : Page 3

As Flash applications move from media to mainstream, it's no longer enough to write code that just works. More prosaic concerns such as readability and maintenance become increasingly important as Flash developers begin to target full-size, mission-critical applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Strategies 4-6
4. Use ActionScript/JavaScript objects.
Object-oriented code is much easier to read (and therefore maintain) than non-OO code, especially as code gets longer. A few OO techniques can go a long way towards clarifying the design underlying your ActionScript code, thereby making it more readable and maintainable. Those unfamiliar with OO design and programming need not fear—some OO techniques are rather simple. As a starter step, use objects to group together related groups of data.

// Document object function Document (title,subject) { this._title = title; this._ subject = subject; } Document.prototype.getTitle = function() { return this._title; } Document.prototype.setTitle = function(val) { this._title = val; }

Dealing with a Document object instead of an array of document data will improve the readability of your code.

Given the scant attention to Flash's object-oriented features in the Flash documentation itself, it is not surprising that object-oriented ActionScript is not a standard. Luckily, there are many articles and tutorials available on OO techniques, and some are ActionScript-specific as well. See the Related Resources section in the left column for more information.

5. Adopt some simple strategies for working with Movie Clips.
First, use the attachMovie method with linkage IDs and avoid using duplicateMovieClip. Using the latter can clutter your workspace with Movie Clips waiting to be duplicated.

Another good technique is to save references to important Movie Clips in ActionScript variables. This allows you to reference them later without having to use long Movie Clip "path-style" references that can easily break. For example, suppose you have a Flash movie about the circus, and you want to place another clown on the interface. If the Movie Clip has the linkage id "clown", you would do this:

_root.workarea.mymovieclip.attachMovie("clown1", "clown",depth);

Now you can get a reference to that clip.

clownClip = mymovieclip ["clown1"];

You can use the clownClip reference later, for example:

clownClip._xscale = 125;

The reference variable is better than using a fragile hard-coded reference as shown below, which will break if you modify the name of workarea or mymovieclip or otherwise rearrange their hierarchy.

_root.workarea.mymovieclip.clown1._xscale = 125;

Often you will want to save references to clips which are duplicated multiple times in arrays, as in:

_root.workarea.mymovieclip.attachMovie("clown1", "clown",depth); var clownClip = mymovieclip ["clown1"]; clownArray.push(clownClip);

Perhaps even better, save the reference in the Clown object instead.

[in Clown object definition] Clown.prototype.getMovieClip = function() { return this._movieClip; } Clown.prototype.setMovieClip = function(val) { this._movieClip = val; } [later, after clownClip created] clown.setClownClip(clownClip);

By storing references in variables and objects, you no longer have to worry about where they reside in the hierarchy of Movie Clips.

Note that the preceding code uses the ActionScript keyword var to show that this is a local variable, not a global one. You should avoid globals as much as possible—they're easy to modify accidentally. I often like to prefix movie clip references with "mc" (as in mcClown) or suffix them with "clip" (as in clownClip) to remind myself and others that the reference is a Movie Clip reference rather than a variable or other object reference.

6. Use Java/JavaScript coding conventions, or strive for another clean, clear style.
Common conventions include camel casing (i.e. camelCasing) variables, standardized indenting, avoiding if() statements without braces, etc. Standardizing programmers' coding styles is important; most of the cost of software is in maintenance, not in development (maintenance accounts for 80% of software costs according to the Code Conventions for the Java Programming Language), and often software is not maintained by the original programmer. Given these two facts, we developers need to do everything we can to improve the readability and comprehensibility of our code.

Because Flash's built-in ActionScript editor is "no-frills," clean code is especially important. The Flash ActionScript Editor lacks advanced analysis and display features common to other code editors. The missing features include syntax highlighting, code-skeleton displays (also called code folding or collapsible code sections), auto-formatting, and refactoring tools. Remember, the programmer who follows in your footsteps will have only the text formatting that you leave behind as a guide. Make your code attractive and consistent by using standardized coding conventions. See the links in the Related References section for links to a couple of style guidelines that describe coding conventions useful for ActionScript.

Code for the Future
Thousand-line or larger Flash movies and months of development rather than days or hours used to be the exception in Flash development; now they're commonplace. The last Flash application I wrote contained 850 lines of ActionScript code and took two months to write. During development the client provided regular feedback on features and usability, and I expect another call any day now, requesting further changes to that application.

In the context of larger projects like these, coding with code maintenance in mind is no longer optional. Organizing ActionScript code protects your work. The sanity you save somewhere down the road may be your own!

Eric Jansson is the lead software engineer at ActiveInk (www.activeink.net), a developer of interactive online curriculum powered by the ActiveInk Network delivery platform. One of his focuses in recent years has been integrating Flash into Web-based applications, to which he brings his years of experience working with Java and object-oriented design. You can reach Eric by .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date