Hack 80 Lock the actions Layer

figs/beginner.gif figs/hack80.gif

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
figs/flhk_1009.gif


Even when the layer is locked, you can:

  • Add keyframes to the locked layer (scripts can be attached only to keyframes). To add a keyframe, select a frame on the locked layer and press F6 (or choose InsertTimelineKeyframe) as you would with any unlocked layer.

  • Attach or edit scripts associated with keyframes on the layer using the Actions panel (F9).

  • Insert frames on the locked layer. To add a frame, select a frame on the locked layer and press F5 (or choose InsertTimelineFrame) as you would with any unlocked layer. Subsequent frames are pushed to the right by the newly inserted frame.

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.

Final Thoughts

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:

  • You are creating a full-length animation with ActionScript control of timelines, such as for The Goober Story (http://www.humbugz.com/hela.htm).

  • You want to create simple timeline control. For example, it's overkill to write a custom class to stop a movie clip at frame 12. It's far better (and more performance efficient) to simply add a stop( ) action at the required frame!

  • You want to attach an event handler to a movie clip instance that doesn't exist on the first frame. You cannot attach an event handler on an instance before it exists. If a movie clip doesn't exist on the timeline until frame 34, you can attach the event handler to it using a script on the actions layer in frame 34, but not in frame 33.

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.