如何访问不同函数内的字符串?

时间:2013-01-20 03:45:01

标签: actionscript-3 flash actionscript

如何访问不同功能内的字符串?我需要访问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
}

1 个答案:

答案 0 :(得分:1)

只需在onLoaded函数之外定义变量即可。然后它可以在其他地方访问:

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];
}