Copy link to clipboard
Copied
Hi there!
Is that possible we assign an array as the class properties?
Code:
package
{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.EventDispatcher;
public class TextToArray
{
public var urlLoader:URLLoader = new URLLoader();
public var dataArray:Array = new Array();
public var textURL:String = "";
public var seperator:String = "";
public function TextToArray(textURL:String,seperator:String)
{
this.textURL = textURL;
this.seperator = seperator;
urlLoader.load(new URLRequest(textURL));//URL to the text file we need to import to Flash
urlLoader.addEventListener(Event.COMPLETE, dataArrayLoaded);
}
public function dataArrayLoaded(e:Event):void
{
this.dataArray = e.target.data.split(seperator);//the seperator such as "\n"
urlLoader.removeEventListener(Event.COMPLETE, dataArrayLoaded);
trace(this.dataArray[2]); // corret Data
}
public function getArray():Array{
trace(this.dataArray[2]); // undefined
trace(textURL); // correct Data
return dataArray;
}
public function getArrayLength():int{
return dataArray.length;
}
}
}
What am I doing wrong?
Thanks for your time.
I believe it will be a matter of calling the function before the data file has had a chance to get loaded and processed.
When declaring your variables you are better off not assigning them values in the declaration like you are doing. Have the Main constructor function assign value to the Array, wait until that processing is done, and then look for the data. Have the TextToArray class dispatch an event when it is does processing and have a listener for it assigned in the Main file. The the eve
...Copy link to clipboard
Copied
When/where are you calling the getArray function?
Copy link to clipboard
Copied
thanks a lot for your prompt reply! I have called it in another class (Main.as) like this:
package
{
import TextToArray;
import VerseID;
import flash.display.MovieClip;
public class Main extends MovieClip
{
...
var textToArray:TextToArray = new TextToArray("data/connection.txt","\n");
var quranData:Array = new Array();
...
public function Main()
{
this.quranData = textToArray.getArray();
trace(this.quranData[2]); // undefined
..........
Copy link to clipboard
Copied
I believe it will be a matter of calling the function before the data file has had a chance to get loaded and processed.
When declaring your variables you are better off not assigning them values in the declaration like you are doing. Have the Main constructor function assign value to the Array, wait until that processing is done, and then look for the data. Have the TextToArray class dispatch an event when it is does processing and have a listener for it assigned in the Main file. The the event handler for that listener process the data into your quranData array.
Copy link to clipboard
Copied
It's undefined because the Event.COMPLETE is not dispatched before you call the getArray function. Wait untill it's called and then call the function.
Copy link to clipboard
Copied
Thanks a lot Ned! You are so right, that is a very LARGE data and this should solve my problem. All the best
Find more inspiration, events, and resources on the new Adobe Community
Explore Now