简体   繁体   中英

Boolean is true in Flash, but false in Javascript

I have this function in Flash:

public function checkFile(file:String):Boolean{
                var b:Boolean;
                var responder:Responder = new Responder(function(reply:Boolean):void{
                    b=reply;
                    msg("vid_"+file+".flv "+ "exists? " + reply);
                    setState("ready");
                    status = "ready";
                },
                    function(res:Object):void{
                        trace("checkFile call failed");
                    });
                mync.call("checkFile",responder,"vid_"+file);
                return b;
            }

I can confirm that the reply variable is true, but return b ends up false:

This is the javascript I use to call the flash function:

function checkFile(){
   alert(thisMovie("vidRecorder").checkFile(currentVid));           
}

And that opens up a message box saying false , while the flash file displays true

What's going on? How can I fix it so that my function returns the same value as reply ?

This happens because the anonymous function in your responder is executed asynchronously.

When your checkFile() function returns the value of b is still false . Eventually, your anonymous function is executed and b gets set to true ... but it's too late in this case, as your checkFile() function has already returned false .

To work around this, you might consider doing this in a slightly different fashion:

When your anonymous function is executed, have it call out to javascript with the asynchronous response. Something like this:

public function checkFile(file:String):void {
    var responder:Responder = new Responder(function(reply:Boolean):void{
        msg("vid_"+file+".flv "+ "exists? " + reply);
        setState("ready");
        status = "ready";
        ExternalInterface.call('someJavascriptFunction', reply);
    },
    function(res:Object):void{
        trace("checkFile call failed");
    });
    mync.call("checkFile",responder,"vid_"+file);
    // note we no longer return a value here
}

In the above code, we call a new Javascript function and provide it the result of the asynchronous operation.

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