Passing variables from actionscript 3 to actionscript 2

98 views Asked by At

I have a swf written in AS3 which loads an AS2 swf.

AS3 code:

var url:String = "as2.swf?myvar=hello";
var spURL:Array = url.split("?");
var urlVars:URLVariables = new URLVariables();
urlVars.decode(spURL[1]);

var req:URLRequest = new URLRequest( spURL[0] );
req.data = urlVars;
req.method = URLRequestMethod.GET;

AS2 code:

trace(_root.myvar);

but nothing traced!

1

There are 1 answers

0
AkisC On

I found solution to my question.

Full source code bellow

AS3: with a movieclip named "as2btn" and a text field named "as3_log". The publish class name is "main_as3"

package  {

    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class main_as3 extends MovieClip {


        public var ld:Loader;

        public function main_as3() {
            // constructor code
            this.as2btn.addEventListener(MouseEvent.MOUSE_DOWN, as2Load);
            this.logMessage("Initialized!");

        }


        public function logMessage(msg:String)
        {
            this.as3_log.appendText("AS3: " + msg + "\n");
        }   

        public function as2Load(evt:MouseEvent)
        {
            if (this.ld) {
                this.ld.unloadAndStop();
                this.ld = null;
            }

            var url:String = "as2.swf?myvar=1rewr&myvar2=1234";
            var spURL:Array = url.split("?");
            var urlVars:URLVariables = new URLVariables();
            urlVars.decode(spURL[1]);

            var req:URLRequest = new URLRequest(spURL[0]);

            req.data = urlVars;
            req.method = URLRequestMethod.GET;

            this.ld = new Loader();
            this.ld.load(req);
            addChild(this.ld);
        }

    }

}

AS2: with a movieclip linkage identifier "main" and class name "main_as2" includes a text field named "as2_log"

class main_as2 extends MovieClip 
{
    var _parent;

    function main_as2() 
    {
        super();
        this.gotoAndStop(1);
        this.logMessage("Initialized!");
        this.logMessage("_parent.myvar: " + _parent.myvar);

    }

    function logMessage(msg:String)
    {
        this["as2_log"].text = this["as2_log"].text + "AS2: " + msg + "\n";
    }
}