1

How to resolve the following error in the mxml program?

The error is

       Loading configuration file /opt/flex/frameworks/flex-config.xml
       /home/tom-j/programs/flex/html/aa.mxml(17):  Error: Access of undefined property addBody.

       ExternalInterface.addCallback("addBody", addBody);

Program is

     <?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

     <mx:Script>
     <![CDATA[

     import mx.controls.Button;
     import mx.controls.Alert;
     import flash.display.InteractiveObject;
     import flash.display.Sprite;
     import flash.media.*;
     import flash.net.*;
     import flash.external.*;
     import flash.external.ExternalInterface;

     //                "javascript function", flash function
     ExternalInterface.addCallback("addBody", addBody);

     public function addBody():void
     {
       Alert.show("Got input from JS");
     }
         ]]>                
     </mx:Script>
     </mx:Application>
Naveen
  • 230
  • 1
  • 3
  • 12

1 Answers1

2

You should try putting this call into an onCreationComplete event handler:

protected function onCreationComplete(event:FlexEvent):void
{
    //just in case to prevent security exceptions:
    Security.allowDomain("*.yourdomain.com");
    Security.allowDomain("localhost");
    ExternalInterface.addCallback("addBody", addBody);
}

The raised error could be because when processing the addCallback line, there did not exist the addBody method.

Update calling the addBody method from javascript

in your html you must have the swf embedded ~like this:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="your_flash_app" width="1000" height="520"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="YourFlashApp.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#bee3f6" />
    <param name="allowScriptAccess" value="always" />
    <embed src="YourFlashApp.swf" quality="high" bgcolor="#bee3f6" width="1000" height="520" name="your_flash_app" align="middle" play="true" loop="false"
        quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer">
    </embed>
</object>

To call the addBody method from javascript, first you must locate the flash application there:

//this only works for sure in IE browsers, but there are workarounds 
//to deal with the others.
var flashApp = document.getElementById("your_flash_app");
flashApp.addBody();
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • How to call the addBody from js now..? – Naveen Apr 15 '11 at 11:53
  • check my update for the sample. note, that afaik `document.getElementById` will return the object you need only in ie browsers (at least this was the situation 2-3 years ago). if you need, i can share my js workaround for the other browsers too – rekaszeru Apr 15 '11 at 12:15
  • I have posted the full code on http://stackoverflow.com/questions/5675870/javascript-error-on-calling-flex-as-function Can u please have alook at it.. – Naveen Apr 15 '11 at 12:16