Home  >  Article  >  Web Front-end  >  Basic tutorial on interactive communication between Javascript and flash_javascript skills

Basic tutorial on interactive communication between Javascript and flash_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:051069browse

Below are some examples of direct communication between Flash and HTML files using Javascript. Each example has simple steps

This article discusses 3 basic methods of Flash/Javascript communication:
Javascript to Flash Communication----Use Flash player's javascript method
Flash to Javascript communication----Use Flash's fscommand
Flash to Flash communication----------Use local connection object Or combine the above 2 technologies

Not all browsers attach great importance to scripts. In order to communicate with the Flash player, the browser must have a built-in hook so that the Flash player can 'listen'. The browser must be The following:
Netscape Navigator 3.0-4.7x, and Netscape 6.2 or higher
(Windows 95/98/NT/2000/XP or MacOS; allows Java and LiveConnect)
Internet Explorer 3.0 or Higher
(Windows 95/98/NT/2000/XP only; ActiveX allowed)


Javascript to Flash communication

This example demonstrates how to use Flash methods Send variables from the HTML input text to the Flash file in the page. The HTML input data is sent to the Flash file through the SetVariable method of Flash.

View example: Example
Download source file: javascript_to_flash .zip(17k)

Steps:

Flash
1. Create a new file and save it as javascript_to_flash.fla
2. Use the text tool to create a text field on the stage
3. Select this text field, in the properties panel, select Dynamic Text from the drop-down list, and fill in "myVar" in the variable column
Note: The best habit is to use Instance , use myVar.text to change the value of myVar. For simplicity and compatibility with Flash4 and Flash5, we use the form of variable names.
4. Save the file
5. Publish HTML files and SWF files

In Dreamweaver
The next step of work is transferred to Dreamweaver, of course it can also be other HTML editors
1. Open the HTML file published in the previous step
2. Insert the generated SWF file and OBJECT /EMBED tag
(1) Insert>Media>Flash, and select this Flash
(2) Switch to the code view, we need to modify the selected and tags
(3) In the OBJECT tag, insert id="myFlash"

codebase="http://download.macromedia.com/ pub/shockwave/cabs/
flash/swflash.cab#version=5,0,0,0"
width=366 height=142 id="myFlash">
Note: If you paste and copy In terms of code, make sure to delete unnecessary line breaks. Otherwise, errors may occur; the id can also be entered directly in the properties panel
(4) In the EMBED tag, insert name="myFlash" and swLiveConnect="true" to ensure The id attribute is not used! The code should look like this:

type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?
P1_Prod_Version=ShockwaveFlash"
name="myFlash" swLiveConnect="true">


3. Create a form field
(1) Return to the design view
(2) Insert a text field (Insert> Form Object> Text Field). If asked whether to add a form field, choose yes
(3) Modify the HTML tag of the text field to this:


Whenever the content of the text field changes, onChange is triggered and the doPassVar() function is called

4. Create a Javascript function that passes variable values ​​
Copy the following Javascript into the tag



5. Save the file and test it (F12)


Flash to Javascript communication

You can send data from HTML to Flash, and vice versa. This example demonstrates how to use Flash's Fscommand to send data to Javascript.

View example: Example 2
Download source file: flash_to_javascript.zip (10K)

Brief steps:
Create a new file in Flash
and save it as flash_to_javascript.fla
Create a text field, set it to Input Text, select "border" so that we can see it, specify its variable as inputVar
Create a button, and add the following as on the button:

on (release) {
fscommand ("send_var", inputVar); <script> <BR><!-- <BR>function doPassVar(args){ <BR>var sendText = args.value; <BR>window.document.myFlash.SetVariable("myVar", sendText); <BR>} <BR>//--> <BR></script>}
保存文件,导出HTML和SWF

Dreamweaver中
1.打开导出HTML文件,修改标签,结果同上:
codebase="http://download.macromedia.com/pub/shockwave/cabs/
flash/swflash.cab#version=5,0,0,0"
width=366 height=142 id="myFlash">

width=366 height=142
type="application/x-shockwave-flash" 

pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?
P1_Prod_Version=ShockwaveFlash" name="myFlash" swLiveConnect="true">

2. 插入如下的Javascript到标签内:
<script> <BR><!-- <BR>var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1; <BR>function myFlash_DoFSCommand(command, args) { <BR>var myFlashObj = InternetExplorer ? myFlash : document.myFlash; <BR>alert (args); <BR>} <BR>if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && <BR>navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) { <BR>document.write('<SCRIPT LANGUAGE=VBScript\> \n'); <BR>document.write('on error resume next \n'); <BR>document.write('Sub myFlash_FSCommand(ByVal command, ByVal args)\n'); <BR>document.write(' call myFlash_DoFSCommand(command, args)\n'); <BR>document.write('end sub\n'); <BR>document.write('</script>\> \n');

//-->



Flash和Flash的通信
通过上面两种方法的混和使用,同一HTML中两个或者更多的Flash直接可以相互传送消息. 从一个flash使用fscommand传送消息给Javascript,使用Flash的Javascript methods把消息传给另外一个flash

详细的看这里:Flash影片之间的通信示例

从Flash MX开始,local connection对象可以用来在flash之间传送消息. 这使得同一HTML中的或者位于两个浏览器窗口中的两个flash影片可以相互发送消息,而不必使用Javascript或者fscommand
详细的看这里:在Flash MX中使用local connection对象(英文)

本站的localConnection教程:http://www.blueidea.com/tech/multimedia/2003/739.asp

附可控制Flash Player的Javascript方法一览表:

Play() ---------------------------------------- 播放动画 
StopPlay()------------------------------------停止动画 
IsPlaying()----------------------------------- 动画是否正在播放
GotoFrame(frame_number)---------------- 跳转到某帧 
TotalFrames()------------------------------- 获取动画总帧数 
CurrentFrame()------------------------------回传当前动画所在帧数-1 
Rewind()-------------------------------------使动画返回第一帧 
SetZoomRect(left,top,right,buttom)-------放大指定区域 
Zoom(percent)------------------------------改变动画大小 
Pan(x_position,y_position,unit)------------使动画在x,y方向上平移 
PercentLoaded()----------------------------返回动画被载入的百分比 
LoadMovie(level_number,path)----------- 加载动画 
TGotoFrame(movie_clip,frame_number)- movie_clip跳转到指定帧数 
TGotoLabel(movie_clip,label_name)------ movie_clip跳转到指定标签 
TCurrentFrame(movie_clip)--------------- 回传movie_clip当前帧-1 
TCurrentLabel(movie_clip)-----------------回传movie_clip当前标签 
TPlay(movie_clip)---------------------------播放movie_clip 
TStopPlay(movie_clip)----------------------停止movie_clip的播放 
GetVariable(variable_name)-----------------获取变量 
SetVariable(variable_name,value)-----------变量赋值 
TCallFrame(movie_clip,frame_number)---call指定帧上的action 
TCallLabel(movie_clip,label)----------------call指定标签上的action 
TGetProperty(movie_clip,property)--------获取movie_clip的指定属性 
TSetProperty(movie_clip,property,number)-设置movie_clip的指定属性

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn