开发者

how to call an external function from inside a class?

i want to call an external function inside a class. thats the code;

in checkConnectionStatus function,

this[_funcNameForSucceededCon].apply(); doesnt work because "this" is the class, not the Application. How can i reach Application at this time or what can i do?

any help will be greatly appreciated.

best regards, mira.

package myLibrary
{
     import air.net.URLMonitor;

     import flash.events.Event;
     import flash.events.StatusEvent;
     import flash.net.URLRequest;

     public class connectionControl
     {
         private var _urlReq:URLRequest;
         private var _urlMonitor:URLMonitor;

         private var _funcNameForSucceededCon:String;
         private var _funcNameForFailedCon:String;

         public function connectionControl(targetURL:String, funcNameForSucceededCon:String, funcNameForFailedCon:String)
         {
             _urlReq = new URLRequest(targetURL);
             _urlMonitor = new URLMoniotor(_urlReq);

             _urlMonitor.addEventListener(StatusEvent.STATUS, checkConnectionStatus);

             _funcNameForSucceededCon = funcNameForSucceededCon;
             _funcNameForFailedCon = funcNameForFailedCon;

             if(_urlMonitor.running == false)
             {
                 _urlMonitor.start();
             }
             else
             {
                 _urlMonitor.stop();
                 _urlMonitor.start();
             }
         }

         private function checkConnectionStatus(e:Event):void
         {
             _urlMonitor.removeEventListener(StatusEvent.STATUS, checkConnectionStatus);

             if(_urlMonitor.available)
             {
                this[_funcNameForSucceededCon].apply();
             }
             else
            开发者_C百科 {
                this[_funcNameForFailedCon].apply();
             }

         }

     }
}


You have passed the name of the function to be serving as a callback. Use instead the function itself and pass it to connectionControl.

 public class connectionControl
 {

     private var _funcSucceededCon:Function;
     private var _funcFailedCon:Function;

     public function connectionControl(targetURL:String, funcSucceededCon:Function, funcFailedCon:Function)
     {
         _urlReq = new URLRequest(targetURL);
         _urlMonitor = new URLMoniotor(_urlReq);

         _urlMonitor.addEventListener(StatusEvent.STATUS, checkConnectionStatus);

         _funcSucceededCon= funcSucceededCon;
         _funcFailedCon= funcFailedCon;

         ...

And:

        if(_urlMonitor.available)
         {
            _funcSucceededCon();
         }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜