构建JS交互接口类
package com.Interactive
{
public class JSInterface
{
public function JSInterface(){
//register javascript function
if (ExternalInterface.available)
{
try
{
//ExternalInterface.marshallExceptions = true;
timeOutExternallAddBack("sendToFlash",recvFromJS);
}
catch (error:SecurityError)
{
throw new Error("A SecurityError occurred: " + error.message + "\n");
}
catch (error:Error)
{
throw new Error("An Error occurred: " + error.message + "\n");
}
}
else
{
throw new Error("External interface is not available for this container.");
}
}
public function recvResponseFromFlash(msg:Object):void
{
try
{
timeOutExternallCall("recvResponseFromFlash", msg);
}
catch(e:Error)
{
throw e;
}
}
public function recvFromJS(msg:Object):void
{
}
/**
* 延时呼叫
* @param func
* @param msg
*
*/
public function timeOutExternallCall(func:String,msg:Object):void
{
setTimeout(function():void{ExternalInterface.call(func,msg);},500);
// timeOutExternallCall(func,msg);
}
/**
* 延时监听
* @param func
* @param msg
*
*/
public function timeOutExternallAddBack(func:String,closure:Function):void
{
setTimeout(function():void{ExternalInterface.addCallback(func,closure)},500);
}
}
}
如上:我们注册了recvFromJS接口函数,用于响应外部javascript调用sendToFlash方法。
注册recvResponseFromFlash接口函数,在flash内部使用时,javascript代码中需定义recvResponseFromFlash函数,用于接收flash传递给javascript的消息。
需要使用该交互类时,如下:
var inter:JSInterface = new JSInterface();即可。
本文介绍如何构建JS交互接口类,实现与Flash之间的交互,包括注册接口函数、响应外部调用及消息传递。
5294

被折叠的 条评论
为什么被折叠?



