试图在swf中注册ExternalInterface.addCallback()

时间:2012-11-20 21:24:56

标签: javascript flash externalinterface

我不是Flash开发人员。我正在尝试将的通信 javascript 连接到 .swf文件。我发现here以及其他最简单方法使用ExternalInterface.addCallback()的地方。我已使用上述链接以及the official docs作为参考。

我很确定我没有太早尝试在flash文件中调用该方法,like this

这是我的HTML(我直接从文档中得到了这个):

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="Project" width="1024" height="768" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
    <param name="movie" value="Project.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#869ca7" />
    <param name="allowScriptAccess" value="always" />
    <embed src="Project.swf" quality="high" bgcolor="#869ca7" width="1024" height="768" name="Project" align="middle" play="true" loop="false" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed>
</object>

我的Javascript:

function sendToActionScript(value) {        
    GetSWF("Project").updateText(value);
}

function GetSWF(strName) {
    if (window.document[strName] != null)
        if (window.document[strName].length == null)
            return window.document[strName];
        else
            return window.document[strName][4];
    else
        if (document[strName].length == null)
            return document[strName];
        else
            return document[strName][5];
}

相关的AS:

public function Project_Main() 
{
    commune();
}

public function commune()
{
    if (ExternalInterface.available) {
        ExternalInterface.addCallback("updateText", updateText);
    }
    else
    {
        var displayText:TextTheme = new TextTheme(_ArialBlack, 100, 100, 800, 500, 42);
        addChild(displayText);
        displayText.text = "ERROR:ExternalInterface.NOTavailable";
    }
}

private function updateText(pid:String, playerName:String):void 
{
    var displayText:TextTheme = new TextTheme(_ArialBlack, 100, 100, 800, 500, 42);
    addChild(displayText);
    displayText.text = "PLAYER ID:" + pid + " PLAYER NAME:" + playerName;
}

我看到here您无法托管C:\的文件,因此我正在托管来自http://localhost的HTML。

当我尝试拨打sendToActionScript("test");时,我收到此错误:

Error: Error calling method on NPObject.

这听起来像是一个权限问题,但正如您所看到的,我有allowScriptAccess=always。我做错了什么?

1 个答案:

答案 0 :(得分:1)

在JS中有一个参数,

GetSWF("Project").updateText(**value**);

但是作为A.S.说它可能是其中两个

private function updateText(pid:String**, playerName:String**):void