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

retrieving variables from external class

Participant ,
Apr 01, 2012 Apr 01, 2012

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

TOPICS
ActionScript

Views

643

Translate

Translate

Report

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 , Apr 01, 2012 Apr 01, 2012

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

...

Votes

Translate

Translate
LEGEND ,
Apr 01, 2012 Apr 01, 2012

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.

Votes

Translate

Translate

Report

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 ,
Apr 01, 2012 Apr 01, 2012

Copy link to clipboard

Copied

PS - you should not be nesting that handlerComplete function within the loadExternalText function.

Votes

Translate

Translate

Report

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
Participant ,
Apr 01, 2012 Apr 01, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

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