【1】Flash控制JS動作
//Flash裡的ActionScript:
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
function myCall(e:MouseEvent){
var intxt = inputa.text;
var inwid = inputa.length;
ExternalInterface.call("fCallJs",intxt,inwid);
}
btn.addEventListener(MouseEvent.CLICK,myCall);
//網頁裡接受的JavaScript:
<script type="text/javascript" >
function fCallJs(aaa,bbb){
alert(aaa+","+bbb);
};
</script>
//注意幾點:
1、如果用Jquery,不要把此
function
放在自執行的$(
function
($) {});裡面,否則不運作。
2、如果在本地測試(沒用IIS),需要把Flash安全設定裡的進階設定,把此swf設為信任檔案,才能看到效果。建議用IIS架個測試網站看。
=======================================================
【2】JS控制Flash動作
//比如在網頁裡的Flash标簽是:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="Test" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="Test.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="Test.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="Test" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
//網頁JavaScript:
<script type="text/javascript" >
$(function($) {
$("#go").click(function(){
var myFlash = getFlashMovieObject("Test");
myFlash.TPlay("star");//在此Flash裡有個叫star的電影片段
});
});
function getFlashMovieObject(movieName) { //這個是為了讓所有浏覽器能擷取到Flash的ID
if (window.document[movieName]){
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1) {
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
} else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}
</script>
//所有JS控制Flash的參數:(注意大小寫)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的指定屬性
其中TGetProperty 和 TSetProperty 的屬性是使用數字0-18來擷取的,下面是各數字代表的屬性:
屬性 作用 數字 使用方式
X Position X坐标 0 Get,Set
Y Position Y坐标 1 Get,Set
X Scale X方向縮放比率 2 Get,Set
Y Scale Y方向縮放比率 3 Get,Set
CurrentFrame movie_clip目前所在幀數 4 Get
TotalFrames movie_clip總幀數 5 Get
Alpha movie_clip的透明度 6 Get,Set
Visibility movie_clip是否可見 7 Get,Set
Width movie_clip的寬度 8 Get
Height movie_clip的高度 9 Get
Rotation movie_clip的旋轉度 10 Get,Set
Target movie_clip的路徑 11 Get
Framesloaded movie_clip已載入的幀數 12 Get
Name movie_clip的實體名字 13 Get,Set
DropTarget movie_clip的拖拽 14 Get
Url 包含movie_clip的動畫的url 15 Get
HighQuality 設定畫面品質為高品質 16 Get,Set
FocusRect 顯示按鈕邊框 17 Get,Set
SoundBufTime 預設聲音緩沖時間 18 Get,Set
============================================================
感謝http://blog.csdn.net/feb13/article/details/7691793
【3】JS調用Flash裡的函數
AS3
import flash.external.ExternalInterface;
ExternalInterface.addCallback("jsf",asf);
//允許js調用flash中的函數 參數1:要調用flash函數的js函數,參數2:被調用flash函數
function asf(vvv){
inputa.text = vvv;
}
JQUERY
$(document).ready(function() {
var myFlash = getFlashMovieObject("Test");
$("#go2").click(function(){
var aaa = $("#input01").val();
myFlash.jsf(aaa);
});
});
function getFlashMovieObject(movieName) {
if (window.document[movieName]){
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1) {
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}else{
return document.getElementById(movieName);
}
}
或者在JQUERY裡這樣
function getFlashMovieObject(movieName) {
if (window.document[movieName]){
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1) {
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}else{
return document.getElementById(movieName);
}
}
function jsf(aaa) {
var myFlash = getFlashMovieObject("Test");
myFlash.jsf(aaa);
};
HTML
<form name="form1" οnsubmit="return false;">
<input type="text" id="input01" value="" />
<input type="button" value="Send" οnclick="jsf(this.form.input01.value);" /><br />
</form>
【4】JS擷取FLASH裡的值,再傳回頁面
JQUERY裡(getFlashMovieObject函數就不寫了,同上)
$("#topBtn4").click(function(){
vr.askInfo();//查詢目前FLASH的狀态
if(flashStatus != "idle"){
vr.vrStop();
}else{
vr.vrPlay();
};
});
//擷取傳回的flashStatus值,由JS調用FLASH裡的函數,再調用回這個函數
function getInfo(fs){
flashStatus = fs;
};
AS3
//允許js調用flash中的函數 參數1:要調用flash函數的js函數名,參數2:被調用flash函數名
ExternalInterface.addCallback("askInfo", askInfo);
function askInfo(){//這個函數是讓JS調用來擷取FLASH狀态資訊的,再傳遞回JS
var fs:String = flashStatus;
ExternalInterface.call("getInfo",fs);
}