Javascript控制swf动画播放

http://arrixlive.wordpress.com/2005/03/25/javascript-in-love-with-flash-control-swf-from-javascript/

Control the play back#

If your swf is mainly a sequence of frames without complex programmatic logic, it is a good idea to control the playback from JavaScript. The following_Flash Methods _are intended for playback control.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* start playing the _root timeline */
flashObj.Play();

//stop playing the _root timeline
flashObj.StopPlay(); 

// Rewind the _root timeline to the first frame
flashObj.Rewind(); 

// returns true if the movie is playing otherwise false
flashObj.IsPlaying(); 

// moves play head of the MovieClip specified
//by "target" to the frame specified by frameNumber.
flashObj.TGotoFrame("target", frameNumber); 

// moves play head of the MovieClip specified
//by "target" to the frame specified by "label", "label" is the frame label you set in the Flash
//authoring environment
flashObj.TGotoLabel("target", "label");

// set the play head to the frame specified by //frameNumber.
flashObj.GotoFrame(frameNumber);

Notice that the frameNumber starts at 0, unlike in Flash where the_ frame_number begins at 1. Before calling this method, you should ensure that the target frame has already been loaded.

1
2
3
4
if (flashObj.PercentLoaded() == 100) {
// frameNumber is assigned somewhere else
flashObj.GotoFrame(frameNumber);
}

Calling a function defined in the Flash movie#

There is no direct way to call a method defined in Flash from _JavaScript_because JavaScript and ActionScript functions and types are not compatible. But there are various ways to trigger code execution in a Flash movie from_JavaScript_.

You can place your ActionScript code in a particular frame (latterly referred to as Frame Function). Then in JavaScript:

1
2
3
flashObj.GotoFrame(frameNumber);
flashObj.TGotoFrame("target", frameNumber);
flashObj.TGotoLabel("target", "label");

These functions control Flash movie playback. Jumping to the specified _frame_will of course trigger the code execution there.

The following two functions execute ActionScript code at specified _frame_without moving the play head.

1
2
flashObj.TCallFrame("target", frameNumber);
flashObj.TCallLabel("target", "label");