• Sending Data from AVM2 (AS3) to AVM1 (AS2/AS1)

    In Chapter 13, we showed how to use a LocalConnection to communicate between an AS3-based movie in Flash Player’s AVM2, and an AS2-based movie in Flash Player’s AVM1. The example was basic, using functions to stop and start an animated movie clip. In this first of future additional examples, we’ll pass data into a function in the AVM! movie. The following code is the additional ActionScript 2.0 added to the avm1.fla file from the Chapter 13 example. Lines 1 through 3 define a function that places a string into a text field on the stage. There is nothing specific to LocalConnection communication. Although you will most likely write specific functions for AVM communication, this example is designed to be relatively flexible. It parses incoming data using the arguments array. AVM1 FLA

    function doIt() {
    avm1TxtFld.text = (“AVM1 function ‘doIt’ called from AVM2,
    with the following arguments: “ + arguments[0] + ” “ + arguments[1]);
    }
    avm2LC.execFunction = function(obj:Object):Void {
    _root[obj.funcName].apply(null, obj.args);

    }

    Lines 5 through 7 comprise the important part of the AVM1 file. They create a function that the LocalConnection instance can access, called execFunction. It uses an object as an argument to allow you to pass a variable amount of data into this conduit. This makes it possible to call different functions with different arguments. The latter process is achieved by first identifying which function to call by parsing funcName from the incoming argument data (_root[obj.funcName], and then using the apply() method to send the incoming arguments to that function. AVM2 FLA Now that the infrastructure has been added to the AVM1 file, we need to add another button to the avm2.fla file. This uses the same process as the book example, using the send() method to send parameters to the AVM1 file via the established local connection. However, in this case, a third parameter is sent containing the name of the function to ultimately be triggered, as well as data to send to that function.

    varBtn.addEventListener(MouseEvent.CLICK, onVarBtn, false, 0, true);
    function onVarBtn(evt:MouseEvent):void {
    avm2LC.send(“avm2″, “execFunction”,
    {funcName:“doIt”, args:["hello1","hello2"]});

    }

    The result is, when clicking on the Var button, the parameters sent from the AVM2 file are used in the AVM1 file.

    Download Data AVM2 to AVM1 From learningactionscript3

    This entry was posted on Tuesday, October 20th, 2009 at 2:46 pm and is filed under ActionScript, Flash, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    You might also like

    AVM2 (AS3) to AVM1 (AS2/1) Communication via LocalConnection The ActionScript virtual machine that runs ActionScript 3 code (AVM2) is completely different from the ActionScript...
    SharedObject in Flash Using AS3 (Flash Cookies) This tutorial will teach you how to use the AS3 SharedObject Class to store small amounts of data on the end user's...
    Connection speed with php One of the hardest script you can find in internet is the one that determines connection speed of a surfer visiting...
    Processing Revolution 3D in Flash Adobe MAX 2010 conference in the keynote for the new Flash Player capabilities introduced, the feature was long...
  • 2 Comments

    Take a look at some of the responses we've had to this article.

    1. Ajit Mediratta
      Posted on March 19th

      Hello Behrouz

      You are great…
      You know what, I’ve been searching for this particular communication from AS3 to AS2 via passing parameters for so many couple of hours.

      And finally some how I found this post….

      I just can say from the deepest heart…THANK YOU.

      You just made my day… GOD BLESS

      Thanks and Regards
      Ajit

    2. Posted on March 19th

      you’re welcome ;)

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message: