Search code examples
actionscript-3javascriptswfobject

AS3 method call from javascript


I'm trying to call an AS3 function from Javascript via SWFObject when i try calling i get this error

cannot call method 'js_method_to_call' of null

import flash.external.ExternalInterface;   
var test_var = ExternalInterface.addCallback("js_method_to_call", moveto);

function moveto() {
gotoAndPlay(1,"Scene 2")

}

and this is my HTML file where i'm using the SWFobject

<head>
        <script type="text/javascript" src="swfobject.js"></script>
        <script type="text/javascript">
            var flashvars = {};
            var params = {};
            var attributes = {};
            attributes.id = "myFlashContent1";
            swfobject.embedSWF("Test_CEO.swf", "containerid", "800", "600", "9.0.0", false, flashvars, params, attributes);
        </script>

        <script type="text/javascript">
        function fall(){
        alert("calling");
        try{
            var myobject = document.getElementById("containerid");
            myobject.js_method_to_call();
        }
        catch(err) {
            alert(err.message);
        }

        }
        </script>
    </head>
    <body>
        <div id="containerid">
            <a href="http://www.adobe.com/go/getflashplayer">
                <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
            </a>
        </div>
         <input id="save file" type="button" value="clickme" onclick="fall();" />
</body>

Reference : Link


Solution

  • I solved this one by running this on XAMPP server

    so yes i attached the swf file like this in my html file

    <object id="myMovie" type="application/x-shockwave-flash" data="test.swf" width="550" height="400">
        <param name="movie" value="test.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" />
    </object>
    

    Then i used the anchor tag to link it to the swf object

    <a id="clicky" href="#">click</a>
    

    Then i called this using this

    <script type="text/javascript">
        var clik= document.getElementById("clicky");
        clik.addEventListener("click",function(){
            document.getElementById("myMovie").letsalertme();
        })
    </script>
    

    So whenever i click the anchor tag the letsalertme() function inside the swf file will get executed.

    And this is my .fla file which contains the actionscript

    import flash.external.ExternalInterface;
    
    function alertMe():void {
    gotoAndPlay(1,"Scene1");
    }
    ExternalInterface.addCallback("letsalertme",alertMe);
    

    So whenever i call the letsalertme() from Javscript this function will call the alertMe function where scene1 will start playing.

    //edit: I achieved this without using swfobject