开发者

Return from inside function

i am noob in actionscript 3 and so i have a problem开发者_开发知识库 and i want your help. I have one function "Login" and function "_urlSended" inside of function "Login". So i want that sub function returned value as a parent function. I just want that script to work

    <![CDATA[

        function onButton1click():void {
            Label1.text = Login('irakli', 'password1');
        }

        public function Login(username:String, password:String){
            var loader:URLLoader = new URLLoader();
            var request:URLRequest = new URLRequest('http://localhost/hosting/index.php');

            request.method = URLRequestMethod.POST;
            var variables:URLVariables = new URLVariables();
            variables.username = username;
            variables.password = password;
            request.data = variables;

            //handlers
            loader.addEventListener(Event.COMPLETE, _urlSended);
            loader.load(request);

            function _urlSended(e:Event){
            var loader:URLLoader = URLLoader(e.target);
            return loader.data;                     
            }
        }


    ]]>


A few things - firstly, we generally reserve Uppercase variables for Class names in AS.

The problem with your code is that you set up a listener and respond to that listener within the same block. You are attempting to return data back to the listener, as it was the calling object, but listener objects have no way of handling it. So make another function, and deal with your data outside of function login:

private var _loader:URLLoader;
public function login(username:String, password:String){
            _loader = new URLLoader();
            var request:URLRequest = new URLRequest('http://localhost/hosting/index.php');

            request.method = URLRequestMethod.POST;
            var variables:URLVariables = new URLVariables();
            variables.username = username;
            variables.password = password;
            request.data = variables;

            //handlers
            _loader.addEventListener(Event.COMPLETE, urlSended);
            _loader.load(request);
        }

private function urlSended(e:Event):void{
    var data:String = _loader.data;  //e.data may work?

    // now do something with it---
}


First off you cannot return stuff from an event handler.

Secondly, you shouldn't have a function within a function. It may work but it is a bad practice.

I suggest you trigger another method with and send loader.data along with it. Either that or do what you to do with loader.data inside your _urlSended function

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜