Monday, February 11, 2008

Controlling the Timeline with simple script in Flash

Timelines really only become useful when you learn how to control them with action script. Otherwise they will just continuously play, looping to the beginning once the end is reached. To stop a timeline at a certain frame all you have to do is place the a stop(); action in a keyframe. It’s a good idea to designate an empty layer as a script layer so you can easily find your script later. Then insert a keyframe at the place you want it to stop and be sure you have the keyframe selected. Then open the Actions Panel (hit F9 to toggle) and add the action script:

stop();

So now you can stop your timeline which will keep it from looping. What if you want to start it playing again though? The script for this is really just as simple but you will also need something to trigger it like a button.

Buttons are pretty easy to make. Simply select an object on the stage (or just draw a box) then right click it and select <<Convert to Symbol>>. This opens a little window in which you need to set the Behavior to Button and click Ok. You now have a button. To add the play(); script to your button be sure you have the button selected and open the Actions Panel and add the action script:

on (release) {
play();
}


You’ll notice that this starts with what is called an event handler. This is what will trigger the action which is contained in the {} brackets. All button handlers start with “on” followed by in this case (release) which tells flash to run the script play(); when the user releases the mouse (basically right after the mouse clicks the button). You won’t see any of this script functioning till you test your movie so give it a try by selecting <<Control>> <<Test Movie>>. Congratulations! You now can control your timeline.

1 comment:

Anonymous said...

great tips. thanks for sharing