简体   繁体   中英

AS3 call function inside loader event function

I have a problem to call a function inside a conditional in another function. I've tried it and it works perfectly but it depends on where the call is made.

Process: I have the checkCode(); function that is called when a form is completed and sent.

private function checkCode():void {
    var url = "checktrue.php";
    var loader:URLLoader = new URLLoader;
    var urlreq:URLRequest = new URLRequest(url);
    var urlvars:URLVariables = new URLVariables;
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    urlreq.method = URLRequestMethod.POST;
    urlvars.codeVar = formattedCode;
    urlreq.data = urlvars;
    loader.addEventListener(Event.COMPLETE, completed); //Completed function
    status.text = "calling loader";
    loader.load(urlreq);
}

When php is complete, "completed" is called and:

private function completed(event:Event = null):void {
    var loader2: URLLoader = URLLoader(event.target);
    var checked:String = loader2.data.checked;
    var codetrue:String = loader2.data.codetrue;

    if(checked == "1" || codetrue == null || codetrue == "") {
        trace("WRONG");
    }
    else{
        download(); //download function
        trace("ok");
    }
}

Here I get the php vars and check if they are valid to call the download() function.

public function download():void {
    req = new URLRequest(proxy + filename);
    file = new FileReference();

    file.addEventListener(Event.COMPLETE, completeHandler);
    file.addEventListener(Event.CANCEL, cancelHandler);
    file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    file.download(req, "file.zip");

}

It is possible this function cannt be called from the "completed" loader function? Thank you very much!

You need to put the download function in another button for the correct use. Or create your own event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM