0

I am new to AS3, and would like to trigger an as3 function from javascript.

AS3

package code {
    import flash.display.MovieClip;
    import flash.external.ExternalInterface;
    import flash.text.TextFormat;

    public class Main extends MovieClip {
        public function Main() {
            ExternalInterface.addCallback("changesize", this.setStyle);
        }

        protected function setStyle() {
            var tf:TextFormat = new TextFormat();
            tf.size = 15;

            editabletext.setTextFormat(tf);
        }
    }
}

html

    <html><head>
            <meta charset="UTF-8">
            <title>animator</title>
            <style type="text/css" media="screen">
            html, body { height:100%; background-color: #ffffff;}
            body { margin:0; padding:0; overflow:hidden; }
            #flashContent { width:100%; height:100%; }
            </style>
        </head>
        <body>
            <div id="flashContent">
                <object type="application/x-shockwave-flash" data="animator.swf" width="550" height="400" id="animator" style="float: none; vertical-align:middle">
                    <param name="movie" value="animator.swf">
                    <param name="quality" value="high">
                    <param name="bgcolor" value="#ffffff">
                    <param name="play" value="true">
                    <param name="loop" value="true">
                    <param name="wmode" value="window">
                    <param name="scale" value="showall">
                    <param name="menu" value="true">
                    <param name="devicefont" value="false">
                    <param name="salign" value="">
                    <param name="allowScriptAccess" value="always">
                    <a href="http://www.adobe.com/go/getflash">
                        <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player">
                    </a>
                </object>
            </div>

        <script type="text/javascript">
            var animator = document.getElementById('animator');
            animator.changesize();
        </script>

    </body></html>

animator.changesize() gives me Uncaught TypeError: animator.changesize is not a function

I have also tried changing allowscript access between sameDomain and always. neither seems to work

Melbourne2991
  • 11,707
  • 12
  • 44
  • 82
  • 1
    Maybe `ExternalInterface.available` is false, if so, you won't get external callbacks. Also it's possible that you call the callback before your SWF is initialized the callback on its side. – Vesper Jul 09 '15 at 07:07
  • 1
    if you are testing locally it won't work, try to test it in a remote server – Özgür Ersil Jul 09 '15 at 07:30

2 Answers2

1

It's a timing issue. The callback will only get added to the DOM once the SWF has been loaded by the Flash Player and the Main constructor is executed, but you are trying to call it immediately when the DOM is parsed.

Since the DOM is always going to be parsed before the SWF is loaded (and the callback is added) you should not assume that the SWF callback is always there and ready to be called. Instead, the SWF should call into JS when it is ready.

Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
0

As mentioned in the comments, first check if ExternalInterface.available is true and test on a server (could be localhost if you set one up locally).

I also notice you have an <object> but no <embed> tag. Some browsers use the object tag, while others use the embed tag. Make sure the id and name for these tags are the same.

Even better, I recommend using swfobject as it takes care of these issues and make the code a bit cleaner. Please have a look at the SWFObject External Interface article and example

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • How can I get the output of externainterface.available from the browser though? – Melbourne2991 Jul 09 '15 at 10:24
  • if ExternalInterface is indeed available, you can make a call(e.g. ```ExternalInterface.call("alert","ExternalInterface.available:"+ExternalInterface.available);``` . An easy thing you can do is add a text field (temporarily as you debug) to the stage and added your debug message (like ExternalInterface.available) there. Normally you'd simply use the ```trace()``` statement to print the data to the console, then if you're using the debug version of Flash Player you should be able to check out the log... – George Profenza Jul 09 '15 at 15:18
  • ...Have a peek [here](http://stackoverflow.com/questions/27791515/how-to-get-trace-output-from-ppapi-content-debugger-flash-player). I'd start with the ExternalInterface sample from swfobject, see how that works, then get the same thing going for the original file – George Profenza Jul 09 '15 at 15:18