1
votes

I try to make an app which connect amfphp with as3 animation. i recieve from amfphp an array which i sort by some variable in 'if' statement. after that i've got 2 arrays with arrays inside each other:

var Array1:Array = [["http://url.to.swf", "http://url.to.link"], ["",""]...];
var Array2:Array = [["http://url.to.swf", "http://url.to.link"], ... ];

after that i run a function that read that arrays and load every swf to the stage:

ladujBajkiGorne(Array1);

function ladujBajkiGorne(polkaGorna:Array):void {
    for(var i=0; i<polkaGorna.length; i++) {
        Aktual = polkaGorna[i];
        var mLoader:Loader = new Loader();
        var mRequest:URLRequest = new URLRequest(String(Aktual[0]));
        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e, String(Aktual[1]) ); } );
        mLoader.load(mRequest);
    }
}

then

function onLoadGorna(e, arg1):void {
   userSwf = e.currentTarget.content;
   userSwf.buttonMode = true;
   userSwf.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) { afterClick(e, arg1 ); });
   booksMC.addChild(userSwf);

   var childName:String = booksMC.getChildAt(childs).name;
   booksMC.getChildByName(childName).x = posX;
   booksMC.getChildByName(childName).y = 0;

   posX += 218;
   childs++;
}

and at the end

function afterClick(e, arg3) {
   var url:URLRequest = new URLRequest(arg3);
   navigateToURL(url, "_self");
}

the problem is that all swf that has been loaded has the same url in the link. it doesn't change in the ladujBajkiGorne function in addEventListener statement. I try many options to change that and nothing works. Any chance to help me with that? Many thanks in advance

2

2 Answers

2
votes

The problem is in asynchronous execution of loader complete handler. It would very good to pass appropriate parameter to loader and read it on handler. I propose a simple solution, correcting your code:

function ladujBajkiGorne(polkaGorna:Array):void {
    for(var i=0; i<polkaGorna.length; i++) {
        Aktual = polkaGorna[i];
        var mLoader:Loader = new Loader();
        var mRequest:URLRequest = new URLRequest(Aktual[0]+"?link="+Aktual[1]);
        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e ); } );
        mLoader.load(mRequest);
    }
}

function onLoadGorna(e):void {
   var link:String = e.currentTarget.parameters.link;
   userSwf = e.currentTarget.content;
   userSwf.buttonMode = true;
   userSwf.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) { afterClick(e, link ); });
   booksMC.addChild(userSwf);

   var childName:String = booksMC.getChildAt(childs).name;
   booksMC.getChildByName(childName).x = posX;
   booksMC.getChildByName(childName).y = 0;

   posX += 218;
   childs++;
}

After that every loader knows about its link.

1
votes

The problem is in the event handler for the complete event. call a function that returns a new function as event handler.

First, Create a function like this:

function getCompleteHandler(url:String):Function {
    return function (e:Event) {
        onLoadGorna(e, url);
    }
}

Then replace this:

mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e, String(Aktual[1]) ); } );

By this:

mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, getCompleteHandler(Aktual[1]) );

That should work now!!