Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Strategies 1-3
1. Place all code in the main timeline (timeline for the _root clip)
This is the single most helpful way to help the programmer who will follow in your footsteps. Rather than attaching huge handler scripts directly to buttons and clips, have a single line of code in the event handlers call a function in the main timeline.

// Good example: Use code like this. on (press) { _root.doSomeAction(); } // Bad example: Avoid code like this. on (press) { [insert lots of code here that forces the developer to move back and forth between this clip and the main script] }

To make the event model of your Flash movie even more apparent, attach the handlers for various events in the main timeline as well. That is, to attach a handler to a button, do the following:

function doSomething () { //event code here } myButton.onpress = doSomething;

The advantage of using this technique is that it makes it obvious to later programmers that an event has been attached to a button, and the programmer no longer has to search through all the buttons to see what handlers are called.

There's no good approach for refactoring existing code using the tools provided by the Movie Explorer...unless all your code is in one place.
Some may argue that it's OK to attach code to buttons and movie clips directly, after all, isn't that why the Flash developers provided the Movie Explorer window? In my opinion, however, the Movie Explorer is more of an indication of the problem than a solution to it. There's no good approach for refactoring existing code using the tools provided by the Movie Explorer. For example, try to search-and-replace all occurrences of a function or movie clip name. The Movie Explorer can't do that unless all your code is in one place.

2. Make complex movies one frame in length.
The purpose of using ActionScript is to manipulate movie elements programmatically. The traditional way of doing this in Flash is to use multiple frames and tweening or other methods for moving these elements on a frame-by-frame basis. However, this practice causes problems with ActionScript-controlled movies. For example, scripts frequently "lose" references to Movie Clips because they aren't in the "current" frame of the movie.

It's tempting to use frames to reflect the state of an application (for example, after the user logs in, go to frame 2), but that can be a dangerous strategy. Movie Clips from other frames will be unavailable in frame 2, and if you try to reference them, you'll get errors. Unless you have a lot to gain from using frames in this fashion, it's best to leave all control to ActionScript. If you want to change the interface, hide the Movie Clip holding the current interface and make the new one visible using script. Turn interface elements on and off.

A bit of esoterica here…you can reference ActionScript functions and variables across frames, but you can't reference Movie Clips across frames. Worse, if you try to store a reference to a Movie Clip in a variable and reference it in another frame, you'll find that the reference is no longer valid! More on this later

3. Handle animations as separate Movie Clips.
This is closely related to strategy #2; if you need to use animations, place the animations in separate movie clips, and then place these clips in the main timeline. That way, the main timeline will still only be one frame in length, while embedded animations can have multiple frames. Having "child" movie clips contain multiple frames is usually fine, and won't cause problems with references to variables and Movie Clips.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap