Using the call() function to create functions

You can't define or call custom functions in Flash Lite as you can in Flash Player 5 and later. However, you can use the call() ActionScript function to execute code that resides on an arbitrary frame in the timeline. This technique lets you encapsulate commonly used code in a single location, making it easier to maintain.

The call() function takes a frame number or frame label as a parameter. For example, the following ActionScript calls the code located on the frame labeled moveUp:

call("moveUp");

The call() function operates synchronously; any ActionScript that follows a call() function call won't execute until all of the ActionScript on the specified frame finishes executing.

To call ActionScript on another frame:

  1. In a new Flash document, insert a keyframe on Frame 10.

  2. With the new keyframe selected, open the Actions panel (Window > Actions), and type the following code:
    trace("Hello from frame 10");
    
  3. Select the keyframe on Frame 1, and in the Actions panel, type the following code:
    stop();
    call(10);
    

    This code stops the playhead on Frame 1, and then calls the code on Frame 10.

  4. Test the application in Adobe® Device Central™.

    You should see "Hello from frame 10" displayed in the emulator.

You can also call code that resides on another timeline, such as a movie clip's timeline. To execute the code, specify the movie clip instance name followed by a colon, and then the frame number or label. For example, the following ActionScript calls the code that resides on the frame labeled moveUp in the movie clip instance named callClip:

call("callClip:moveUp");

This technique is often used to create call clips or function clips--movie clips whose sole purpose is to encapsulate regularly used code. A call clip contains a keyframe for each function you want to create. You typically label each keyframe according to its purpose. Adobe also recommends that you create a new layer for each new keyframe, and that you give each layer the same name as the frame label you assign to the keyframe.

The following figure shows the Timeline of an example call clip. The first keyframe of a call clip always contains a stop() action, which ensures that the playhead doesn't continually loop over the frames in its Timeline. Subsequent keyframes contain code for each "function." Each function keyframe is labeled to identify what it does. To make editing and viewing the call clip easier, each function keyframe is typically inserted on a separate layer.

The following procedure explains how to create and use a call clip.

To create and use a call clip:

  1. In Adobe® Flash® CS3, create a new document from the Flash Lite 1.1 Symbian Series 60 document template.
  2. Select Insert > New Symbol.
  3. In the Create New Symbol dialog box, type Call Clip in the Name text box, and then click OK.

    The movie clip opens in editing mode.

  4. Click the Add New Layer button the Timeline window twice to insert two new layers.

    Name the top layer Actions, the second layer function1, and the third layer function2.

  5. Insert a keyframe on Frame 2 of the function1 layer, and another keyframe on Frame 3 of the function2 layer, as the following figure shows:

  6. Select the keyframe on the Actions layer and open the Actions panel.
  7. Add a stop() action to the Actions panel.
  8. Select the keyframe on Frame 2 of the function1 layer and do the following:
    1. In the Property inspector, type function1 in the Frame Label text box.

    2. In the Actions panel (Window > Actions), type the following code:
      trace("function1 was called.");
      
  9. Select the keyframe on Frame 3 of the function2 layer and do the following:
    1. In the Property inspector, type function2 in the Frame Label text box.

    2. In the Actions panel (Window > Actions), type the following code:
      trace("function2 was called.");
      
  10. Press Control+E (Windows) or Command+E (Macintosh) to return to the main Timeline.
  11. Set your document's view to include the work area around the Stage by selecting View > Work Area.

    Because the call clip doesn't need to be visible to the user, you can place it in the work area.

  12. Open the Library panel (Window > Library) and drag the Call Clip symbol to the work area around the Stage.

    The call clip doesn't contain any visual elements so it appears on the Stage as a small circle, representing the movie clip's registration point.

    TIP

    To make your call clip more easily identifiable on the Stage, add some text or other visual element to the first keyframe in the call clip's Timeline.

  13. In the Property inspector, type callClip in the Instance Name text box.
  14. In the Timeline, select Frame 1 on the layer named ActionScript.
  15. In the Actions panel, enter the following code:
    call("callClip:function1");
    call("callClip:function2");
    
  16. Test your application in the emulator (Control > Test Movie).

    You should see the following text:

    function1 was called.
    function2 was called.