1

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

that's really simple solution. Thanks for that. It helps me a lot!
Yeah!, its a very nice solution
1

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!!

1 Comment

your answer is also correct but Serge Him do it more simply. i'm appreciate your time. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.