Tuesday 25 March 2008

Controlling timeline events

In Flash, if you want to stop, play or move to a certain frame in the movie you have to use actionscript.

First - stop. To stop a flash movie you simply have to write stop() in the actions panel of the frame you want the movie to stop at.


Play. This is just as easy as the stop function, just write play(). flash plays movies automatically so you won't need to use this command as much as stop.

To move forward and backward one frame use nextFrame(), to go forward, and prevFrame(), to go backwards.

The most useful of the timeline controls are the gotoAndStop(num) and the gotoAndPlay(num) functions. To use them just replace "num" with the frame number that you want to play or stop, or even you can replace it with a frame label in "".

Lets put this in to action with a demo.





Click on the buttons to effect the movie.


The code is as follows : -
import flash.events.MouseEvent;
playbut.addEventListener(MouseEvent.MOUSE_DOWN, playFunction);
stopbut.addEventListener(MouseEvent.MOUSE_DOWN, stopFunction);
nextframebut.addEventListener(MouseEvent.MOUSE_DOWN, nextframeFunction);
prevframebut.addEventListener(MouseEvent.MOUSE_DOWN, prevframeFunction);
function playFunction(Event:MouseEvent):void {
play()
}
function stopFunction(Event:MouseEvent):void {
stop()
}
function nextframeFunction(Event:MouseEvent):void {
nextFrame()
}
function prevframeFunction(Event:MouseEvent):void {
prevFrame()
}

I'm using the same button code from my last tutorial, but replacing the trace("") function with the functions i have talked about in this tutorial.

explaining the code:-
line 1 - importing the Mouse Events
line 2 I'm adding event listeners to the buttons instance names that i have. When the mouse is touching playbut and it is pressed, do the playFunction.
line 3-5 same as line 2 but for different functions.
line 6-8 This is the playfunction, which is called every time the playbut is pressed. it makes the flash movie play, as i have explained earlier in the post.
9-17 These are the other functions, called by the event listeners. 

Well, that's it for timeline events.

If your unsure about buttons, check out my earlier tutorial here 

1 comment:

Adam Feneley said...

Very helpful, thanks :)