2011-01-22 50 views
2

问题Javascript什么时候可以开始调用Actionscript?

是否有非轮询的方式为Javascript命令闪存权当它的外部接口准备好了吗?

背景

在ActionScript中,我已经注册了一个功能的Javascript调用:

ExternalInterface.addCallback('doStuff", this.doStuff); 

我用SWFObject将Flash嵌入到我的网页:

swfobject.embedSWF(
    'flash/player.swf', 
    'flashPlayer', 
    '100%', 
    '100%', 
    '9', 
    'expressInstallSwfTODO.swf', 
    {}, 
    {allowfullscreen: true}, 
    {}, 
    function(status) { 
     if (!status.success) { 
      alert('Failed to embed Flash player'); 
     } else { 
      $('flashPlayer').doStuff(); 
     } 
    }.bind(this) 
); 

当Flash通过回调成功嵌入时,SWFObject允许您运行代码。我尝试在此回调中运行$('flashPlayer')。doStuff,但它声称它未定义。看来Flash需要一些时间来启动它的外部接口。所以我一直在使用轮询攻击来找出外部接口什么时候准备就绪:

new PeriodicalExecutuer(
function(poller) { 
    if ($('flashPlayer').doStuff) { 
    $('flashPlayer').doStuff(); 
    poller.stop() 
    } 
}, 
0.2 
); 

这个轮询器并不理想。在doStuff的执行过程中存在视觉上可察觉的延迟,并且它使我的整体代码结构变得模糊。

+0

的onload会平滑但速度较慢。拿你的选择。 – 2011-01-22 23:49:14

回答

4

在Javascript中:

function flashIsReady() 
{ 
    $('flashPlayer').doStuff(); 
} 

在ActionScript:

if (ExternalInterface.available) { 
    ExternalInterface.addCallback('doStuff', this.doStuff); 
    ExternalInterface.call("flashIsReady"); 
} 
+0

如果在运行此AS时ExternalInterface不可用,该怎么办? – JoJo 2011-01-23 00:27:44

0

我做了一个投票的解决方案。在动作我有这样的功能:

private function extIsInterfaceReady():Boolean { 
    return ExternalInterface.available; 
} 

而在JavaScript中,“onFlashReady”事件我也编入intialization后,我开始的间隔是这样的:

this.poll_flash = setInterval(function() { 
    if (typeof this.flash_obj === 'undefined') { 
     return false; 
    } 

    if (typeof this.flash_obj.isInterfaceReady === 'undefined') { 
     return false; 
    } 

    if (this.flash_obj.isInterfaceReady()) { 
     clearInterval(this.poll_flash); 
     return this.continueOn(); 
    } 
    }, 100); 
相关问题