JavaScript Methods for Flash Objects


JavaScript Methods for Flash Objects

The Flash plug-in and ActiveX control have built-in methods that can be accessed by JavaScript. For the Flash methods to work in JavaScript, the <OBJECT> must contain the ID attribute, and the <EMBED> tag must contain the NAME attribute. The values should be the same in both to avoid compatibility problems. It's important to note that with all the capabilities of Flash 5, it's going to be pretty rare that you need to use Flash methods. That doesn't mean that you won't ever use them, but most of these capabilities are more easily handled inside Flash. They're included here for those of you who are still creating movies in Flash 5 that need to be compatible with Flash 4.

After the Flash plug-in has loaded, the following standard methods are accessible to client-side JavaScript:

  • GetVariable("variableName"). Returns the value of the Flash variable variableName. Syntax: window.document.yourMovie.GetVariable ("variableName");

  • SetVariable("variableName","value"). Sets the Flash variable variableName to the value specified by the value variable. Syntax: window.document.yourMovie.SetVariable("VariableName", value); .

  • LoadMovie(layerNumber,"url"). Causes Flash to load the movie specified by url to the layer specified by layerNumber. Syntax: window.document.yourMovie.LoadMovie(layerNumber, "http://www.yourURL.com/"); .

  • IsPlaying(). Returns true if the movie is currently playing. Syntax: window.document.yourMovie.IsPlaying(); .

  • TotalFrames(). Returns the total number of frames in the movie. Syntax: window.document.yourMovie.TotalFrames(); .

  • PercentLoaded(). Returns the percent of the Flash movie that has downloaded to the Web browser so far. The range is from 0 to 100. Syntax: window.document.yourMovie.PercentLoaded(); .

  • Play(). Starts playing the movie. Syntax: window.document.yourMovie.Play(); .

  • StopPlay(). Stops playing the movie. Syntax: window.document.yourMovie.StopPlay(); .

  • Rewind(). Moves the playhead to the first frame of the movie. Syntax: window.document.yourMovie.Rewind(); .

  • GotoFrame(frameNumber). Moves the play head in the main timeline to a specific frame. The values of frameNumber begin with 0, so to go to frame 2, the value would be 1. So that you don't request a frame that has not yet been loaded, you should use the PercentLoaded() method to make sure that the desired frame is available. Syntax: window.document.yourMovie.GoToFrame(frameNumber); .

  • Zoom(percent). Zooms the movie using the percent variable as a relative scale factor. For example, Zoom(50) will double the size of the objects in the view, while Zoom(200) will cut in half the size of objects in the view. Zoom(0) will reset the view to 100%. It is not possible to zoom out any more than 100% (normal size ). Syntax: window.document.yourMovie.Zoom(percent); .

  • SetZoomRect(left,top,right,bottom). Zooms in on a rectangular area of the movie. The units for each variable are in twips (1/20 of a pixel). There are 1440 units per inch, so, to calculate the coordinates from within Flash, set the ruler units to points and multiply the coordinates by 20. Syntax: window.document.yourMovie.SetZoomRect(left,top,right,bottom); .

  • Pan(x,y,mode). Tells Flash to pan a movie if it is zoomed in. Negative x values cause the movie to pan to the left, and negative y values cause the movie to pan to the top. If the mode variable is set to 0, the x and y values are treated as pixels. If it is set to 1, the x and y values are treated as a percentage of the browser window size. Flash will not pan beyond the boundaries of the zoomed-in movie. Syntax: window.document.yourMovie.Pan(x,y,mode); .

In addition to the standard methods, you have access to the Tell Target methods. The Tell Target methods let you control movie clips on the timeline in Flash.

  • TCallFrame("movieClipInstance",frameNumber). This executes the actions in the numbered frame of the movie clip instance. In call actions, the playhead does not move to the frame; just the actions are executed. Syntax: window.document.yourMovie.TCallFrame("movieClipInstance", frameNumber); .

  • TCallLabel("movieClipInstance","label"). This executes the actions in the labeled frame of the movie clip instance. In call actions, the playhead does not move to the frame; just the actions are executed. Syntax: window.document.yourMovie.TCallLabel("movieClipInstance", frameLabel); .

  • TCurrentFrame("movieClipInstance"). This returns the frame number where the playhead is located in the timeline of the specified movie clip instance. It is zero-based , so if the playhead is on frame 1, the value 0 will be returned. Syntax: window.document.yourMovie.TCurrentFrame("movieClipInstance"); .

  • TCurrentLabel("movieClipInstance"). This returns the label name of the current frame of the timeline of the specified movie clip instance. If that frame doesn't have a label, an empty string is returned. Syntax: window.document.yourMovie.TCurrentLabel("movieClipInstance"); .

  • TGotoFrame("movieClipInstance",frameNumber). On the timeline of the specified movie clip instance, the playhead is moved to the frame number specified by frameNumber. Syntax: window.document.yourMovie.TCurrentFrame("movieClipInstance", frameNumber); .

  • TGotoLabel("movieClipInstance""label"). On the timeline of the specified movie clip instance, the play head is moved to the frame with the label specified by label. Syntax: window.document.yourMovie.TGotoLabel("movieClipInstance","labelName"); .

  • TPlay("movieClipInstance"). This plays the timeline of the specified movie clip instance. Syntax: window.document.yourMovie.TPlay("movieClipInstance"); .

  • TStopPlay("movieClipInstance"). This causes the timeline of the specified movie clip instance to stop. Syntax: window.document.yourMovie.TStopPlay("movieClipInstance");.

  • TGetProperty("movieClipInstance",property). A string containing the requested property is returned from the timeline of the specified movie clip instance. Syntax: window.document.yourMovie.TGetProperty("movieClipInstance",property); .

  • TSetProperty("movieClipInstance",property,value). Flash sets the value of the property on the timeline specified by the movie clip instance. Syntax: window.document.yourMovie.TSetProperty("movieClipInstance",property,value); .

If you plan to use any of the Flash methods, it's best to publish your HTML file using the Flash with FSCommand template because it will automatically set up everything you need. If you look in the HTML, you will see at one point a variable created that starts with the name of your FLA and ends with Obj (for example, myFLAObj). Just make sure that you copy and paste that line into any function that needs to use Flash methods. So, when you actually call the methods, it will be like this:

 myFLAObj.SetVariable("/foo", "bar"); 


Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net