Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can we assign "Arrays" as class properties??

Contributor ,
Jan 14, 2014 Jan 14, 2014

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.

TOPICS
ActionScript
567
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 14, 2014 Jan 14, 2014

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

...
Translate
LEGEND ,
Jan 14, 2014 Jan 14, 2014

When/where are you calling the getArray function?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 14, 2014 Jan 14, 2014

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

       ..........

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 14, 2014 Jan 14, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2014 Jan 14, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 14, 2014 Jan 14, 2014

Thanks a lot Ned! You are so right, that is a very LARGE data and this should solve my problem. All the best

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines