Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an ActionScript function via Javascript

I'm trying to call a function in an action script using the ExternalInterface.addCallback API, but I can't seem to get it to work. Here's what I have:

ActionScript:

//MyClass.as  
package {

    import flash.display.Sprite;
    import flash.external.ExternalInterface;

    public class MyClass extends Sprite
    {
        public function MyClass()
        {
            ExternalInterface.addCallback('getStringJS', getStringAS);
        }

        public function getStringAS():String
        {
            return "Hello World!";
        }
    }
}

NOTE: I'm compiling this into an swf using the flex mxmlc compiler if that matters.

HTML/Javascript:

<!doctype html>
<html>
    <head>
        <title>User Identification</title>
    <head>
    <body>
        <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
            <param name="movie" value="MyClass.swf">
            <embed src="MyClass.swf" width="1" height="1">
        </object>
        <script type="text/javascript">
            var flash = document.getElementById("MyClass");
            var str = flash.getStringJS();
            alert(str);
        </script>
    </body>
</html>

The error I'm getting is:

Uncaught TypeError: Object #<HTMLObjectElement> has no method 'getStringJS'

I also tried adding in a timeout in case the swf file wasn't loading, but I didn't have any success with that method either.

Any thoughts?

Cheers,
Mike

like image 663
Swift Avatar asked Jun 01 '11 22:06

Swift


People also ask

Is ActionScript JavaScript?

Adobe® ActionScript® 3.0 is a programming language like JavaScript—both are based on ECMAScript. ActionScript 3.0 was released with Adobe® Flash® Player 9 and you can therefore develop rich Internet applications with it in Adobe® Flash® CS3 Professional, Adobe® Flash® CS4 Professional, and Adobe® Flex™ 3.

What is the difference between ActionScript and JavaScript?

Both JavaScript and ActionScript are scripting languages, or interpreted languages. The browser (or Flash) interprets the code at runtime. They are not compiled (like C++ or Java). ActionScript is used in Flash applications, while JavaScript can run on almost any web page.

Is ActionScript difficult?

ActionScript may be difficult to learn for beginners with no programming experiencing, however, it becomes easier to grasp with repeated practice and use.


1 Answers

I figured it out. The key way to signal the javascipt through ExternalInterface.call so we know for sure that the swf is loaded. The most "Universal" way to do this is as follows:

MyClass.as

//MyClass.as  
package {

    import flash.display.Sprite;
    import flash.external.ExternalInterface;

    public class MyClass extends Sprite
    {
        public function MyClass()
        {
            ExternalInterface.addCallback('getStringJS', getStringAS);
            if  (ExternalInterface.available) {
                ExternalInterface.call("isConnectedFlex");
            }
        }

        public function getStringAS():String
        {
            return "Hello World!";
        }
    }
}

index.html

<!doctype html>
<html>
    <head>
        <title>User Identification</title>
    <head>
    <body>
        <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
            <param name="movie" value="MyClass.swf">
            <embed src="MyClass.swf" width="1" height="1">
        </object>
        <script type="text/javascript">

            var flash = document.getElementById("MyClass");

            function isConnectedFlex() {
                var str = flash.getStringJS();
                alert(str);
            }


        </script>
    </body>
</html>
like image 111
Swift Avatar answered Oct 06 '22 00:10

Swift