Taylor Swift
Taylor Swift

Reputation: 242

How to access a string inside a different function?

How do I access a string that is inside a different function? I need to access myArrayOfLines.

public var myTextLoader:URLLoader = new URLLoader;

myTextLoader.addEventListener(Event.COMPLETE, GameEnter.onLoaded) //GameEnter is the document class
myTextLoader.load(new URLRequest("Test.txt")); //loads the text file Test.txt

public static function onLoaded(e:Event):void 
{
    var myArrayOfLines:Array = e.target.data.split(/\n/); //splits it up every new line
    trace(myArrayOfLines); //trace the text in Test.txt
    //Test.txt contains the word "Test"
}



public function foo()
{
    //how do i access myArrayOfLines? Example below
    //Name.text = ""+myArrayOfLines; DOES NOT WORK
}

Upvotes: 0

Views: 69

Answers (1)

user1901867
user1901867

Reputation:

Just define the variable outside of the onLoaded function. Then it can be accessed elsewhere:

var myArrayOfLines:Array;

public function onLoaded(e:Event):void 
{
    myArrayOfLines = e.target.data.split(/\n/); //splits it up every new line
    //...etc
}

public function foo()
{
    //display first item in array
    Name.text = ""+myArrayOfLines[0];
}

Upvotes: 1

Related Questions