1

Could you give me some idea of how to give value to a variable from a function?

What I do is load the content of a txt, but I need that instead of feeding a dynamic text (that works), feed a variable that I can use outside the function. I want to give the content of the txt to the variable "textito" declared outside the function.

Any ideas?

The text "teet1" is only for a test, what I really need is to give value to the variable "textito" because I need to use it outside the function. Lo que ha

override public function SetData(xmlData:XML):void 
    {           
       for each (var element:XML in xmlData.children())
       {

        if (element.@id == "f0")        
          {
            var textito:String
            var f0 = [email protected]();
            var f0A:String = f0.substr(-4);
            var loader:URLLoader = new URLLoader(new URLRequest("C:/Users/Nicolás Agüero/Desktop/Test/" + f0));
            loader.addEventListener(Event.COMPLETE, onFileLoaded);
            function onFileLoaded():void
                {
                    textito = loader.data;
                }

            teet1.text = textito;

        } 

        }

    }
1
  • 2
    Don't declare var textito:String;. If there's a local function variable with the same name as external variable, then the function will address the local one. Commented Sep 2, 2019 at 1:34

1 Answer 1

1

Any variables that need global access should be declared outside of any functions.

Try a setup like this:

public var textito :String = "";
public var loader :URLLoader;

override public function SetData(xmlData:XML) :void 
{           
    for each (var element:XML in xmlData.children())
    {
        if (element.@id == "f0")        
        {
            var f0:String = [email protected]();
            var f0A:String = f0.substr(-4);

            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, onFileLoaded);
            loader.load( new URLRequest("C:/Users/Nicolás Agüero/Desktop/Test/" + f0) );
        }

    }
}

public function onFileLoaded(evt:Event) :void
{
    textito = loader.data; //can also try... evt.data ...since the Event gives Loader its data.
    teet1.text = textito;
}
Sign up to request clarification or add additional context in comments.

Comments

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.