本文介绍了在 Flash 中使用 ExternalInterface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑一些 flash 以进行外部 javascript 函数调用,但没有成功.这是我的 actionscript 2.0 代码:

I'm trying to edit some flash to make an external javascript function call, but with no success. Here's my actionscript 2.0 code:

//testing external .js calls

import flash.external.ExternalInterface;

//attempting to make external js call

ExternalInterface.call("createPlaylist","It's my Life!");

这是我的 javascript;

and here's my javascript;

function createPlaylist(mess){
  alert("called createPlaylist: " + mess);
}

我看过很多例子,主要是对ExternalInterface.addCallback 的使用感到困惑.我不需要 javascript 将任何东西返回给 flash,所以这是必要的吗?

I've seen lots of examples and I'm mainly confused about the use of ExternalInterface.addCallback. I don't need the javascript to return anything to flash, so is this necessary?

无论出于何种原因,我从未看到警报.有人在我的代码中看到任何问题吗?是否有一些我没有的 ExternalInterface 库?另外,使用 ExternalInterface 的最佳方法是什么(即;错误检查等)提前致谢...

For whatever reason, I never see the alert. Does anyone see any problems in my code? Is there some ExternalInterface library I don't have? Also, what's the BEST way to use ExternalInterface (ie; error checking, etc.) Thanks in advance...

推荐答案

ExternalInterface.addCallback 用于 javascript 能够调用您的 Flash 应用程序.例如,如果您想要一个开始/停止视频的 HTML 按钮,您只需为命名方法添加回调,您的 js 就可以调用 [FlashObject].callback 方法名称.

ExternalInterface.addCallback is for javascript to be able to call into your Flash application. If for example you want a HTML button that starts/stops a video you just add a callback for a named method and your js can than call [FlashObject].callback method name.

我认为在应用程序中添加 ExternalInterface 方法的最佳方法是为应用程序中的每个交互案例设置一个负责 JS 通信的类.例如:

I would say that the best way to add ExternalInterface methods in your application is to set up a class responsible for JS communication for each interaction case in the app. For example:

public class ExternalVideoControl {

    private var video:MediaDisplay;

    public function ExternalVideoControl(video:MediaDisplay) {
        //ExternalInterface.addCallback  - one callback for each method you want to expose, pointing to a method within this class;
        //add listeners on the video player and point them to methods in this class, for example onProgress
    }
    public function playVideo():void {
        //play the video on the mediaDisplay
    }
    private function onProgress(event:ProgressEvent):void {
        //ExternalInterface.call - report progress back to javascript
    }
}

要更直接地测试 ExternalInterface,请尝试调用

To test ExternalInterface more directly, try calling

ExternalInterface.call("alert", "Hello World!");

这篇关于在 Flash 中使用 ExternalInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:15