Copy link to clipboard
Copied
I have created a loader class that retrieves variables from an external text file. I call the loader class and all works well but I cannot figure out how to retrieve and use the variables in the Main class. Can someone help me please?
Call from Main class:
function createInfoText():void{
var loadTxt:LoadTxt= new LoadTxt("text_files/mainTxt.txt");
}
LoadTxt Class:
package app.gui
{
import flash.net.*;
import flash.events.*;
public class LoadTxt
{
private var externalTxt:String;
public var loader:URLLoader=new URLLoader();
public function LoadTxt(external)
{
externalTxt= external;
init();
}
function init():void{
loadExternalText();
}
function loadExternalText():void{
loader.addEventListener(Event.COMPLETE,handlerComplete);
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
//Attmept to load the data
loader.load(new URLRequest(externalTxt));
function handlerComplete(evt:Event):void {
var loader:URLLoader=URLLoader(evt.target);
trace(loader.data) //this works OK
}
}
}
}
I know I have to get the data that is held in the var "loadTxt" but I am brain dead!!
Thanks
Here's some things to think about... The LoadTxt class needs to have loaded the file before the main file can try to access it. So the LoadTxt class should have some way of telling the main file it is ready. What you could do in your handlerComplete function is dispatch an event to that effect, and have an event listener assigned to the LoadTxt instance so that the data can be extracted from it when it is ready.
You could use a custom event class in the LoadTxt class to pass the data with the
...Copy link to clipboard
Copied
Here's some things to think about... The LoadTxt class needs to have loaded the file before the main file can try to access it. So the LoadTxt class should have some way of telling the main file it is ready. What you could do in your handlerComplete function is dispatch an event to that effect, and have an event listener assigned to the LoadTxt instance so that the data can be extracted from it when it is ready.
You could use a custom event class in the LoadTxt class to pass the data with the event you dispatch, though you'll need to read up on creating/using one that supports passing parameters.
Copy link to clipboard
Copied
PS - you should not be nesting that handlerComplete function within the loadExternalText function.
Copy link to clipboard
Copied
Ok, that sounds like a way to do it. Thanks, I guess I have some homework to do. I may need some more help but I will give it a go first before I ask again.