|Table of Contents|
Hack 80 Lock the actions Layer
ActionScript can be placed almost anywhere on the timeline. Keep your scripts on a dedicated, locked actions layer to promote separation of code from assets.
In animation applications, the timeline represents the passage of time. Flash generates an event (onEnterFrame) every time the next frame interval has started. That makes perfect sense until you start adding code into the mix. Unlike other scripting languages in which scripts are stored centrally, Flash code can be attached to keyframes anywhere on the timeline, usually in the order in which it needs to run, using the Actions panel.
Some developers choose to centralize their ActionScript on frame 1 of the main timeline, but in many projects, at least some code is attached to keyframes other than frame 1. Many developers even attach code to buttons and movie clips on various layers. As the timeline becomes long and many layers are added with code scattered throughout, the Flash movie becomes more difficult to debug and maintain. Developers often need to use the Movie Explorer (WindowOther PanelsMovie Explorer) just to locate their code!
To minimize the problem, name the first layer in your timeline actions (or scripts) and attach all your scripts only to that layer. Using a dedicated layer just for scripts helps to centralize your code (you can use the same layer for any frame labels you add via the Properties panel or create a labels layer for that purpose).
Instead of attaching code directly to buttons and movie clips on other layers, give your buttons and movie clips instance names using the Properties panel. Then you can "attach" code to a movie clip indirectly by using its instance name in code written elsewhere. The ActionScript code should be placed on the actions layer, not on the layer containing the clip, nor on the clip's timeline. For example, if a movie clip has the instance name ballClip, you can refer to it from code on the actions layer of the main timeline and set its x coordinate as follows:
var ballClip:MovieClip; ballClip._x = 50;
Locking a Layer
One simple trick to keep your actions layer as a script-only layer is to keep it permanently locked. Flash allows you to lock any layer in the timeline by clicking the dot in the padlock column that appears adjacent to the layer, as seen in Figure 10-9. To unlock a layer, click the padlock icon that indicates a locked layer (in Figure 10-9, Layer 2 is locked). Locking a layer doesn't prevent the layer from being edited, as you might assume; however, it does prevent anything from being added onto the Stage in the locked layer.
Figure 10-9. To lock a layer, click the dot in the padlock column
Even when the layer is locked, you can:
So, locking the actions layer prevents you from drawing graphics in it or attaching symbols to its keyframes (thus keeping it a script-only layer). However, you can add and edit keyframes and scripts, even though it remains locked.
Although storing code in external .as files is appropriate and recommended in many applications, as is storing all your code in the first frame of the main timeline when possible, this is just not practical when:
Even when such issues tend to spread out your code, you can still keep the majority of your code on frame 1 of the actions layer if you write all your scripts and event handlers as functions and call the functions on the frames on which they are required. This keeps most of your ActionScript on frame 1.
|Table of Contents|