View comments | RSS feed

Timeline variables

Timeline variables are available to any script on that particular timeline. To declare timeline variables, use the var statement and initialize them in any frame in the timeline. The variable is available to that frame and all following frames, as shown in the following example.

To use timeline variables in a document:

  1. Create a new Flash document, and name it timelinevar.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    var myNum:Number = 15; /* initialized in Frame 1, so it's available to all frames */
    
  3. Select Frame 20 of the Timeline.
  4. Select Insert > Timeline > Blank Keyframe.
  5. With the new keyframe selected, type the following ActionScript into the Actions panel:
    trace(myNum);
    
  6. Select Control > Test Movie to test the new document.

    The value 15 appears in the Output panel after approximately a second. Because Flash documents loop by default, the value 15 continually traces in the Output panel every time the playhead reaches Frame 20 in the Timeline. To stop the looping action, add stop(); after the trace() statement.

You must declare a timeline variable before trying to access it in a script. For example, if you put the code var myNum:Number = 15; in Frame 20, any scripts attached to a frame before Frame 20 cannot access myNum and are undefined instead of containing the value 15.


Flash CS3


Comments


Varactor said on Dec 5, 2007 at 4:05 AM :
make sure you dont put

var myNum:Number;

in a function otherwise it want be available in the other frames.

hope this helps,
Oliver

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000664.html