Question

I am not a flash developer. I am trying to wire up communication from javascript to a .swf file. I found here and other places that the simplest method was to use ExternalInterface.addCallback(). I have used the above link as well as the official docs as reference.

I am pretty sure that I am not trying to call the method in the flash file too early, like this.

Here is my HTML (I got this straight from the documentation):

<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>

My 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];
}

The relevant 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;
}

I read here that you cannot have your files hosted from C:\ so I am hosting the HTML from http://localhost.

When I try to call sendToActionScript("test"); I get this error:

Error: Error calling method on NPObject.

It sounds like a permission problem, but as you can see, I have allowScriptAccess=always. What am I doing wrong?

Was it helpful?

Solution

in JS there is 1 parameter,

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

but as A.S. says it sould be two of them

private function updateText(pid:String**, playerName:String**):void 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top