Skip to main content
Participant
June 16, 2011
Question

Problem referencing a newly created class instance - I don't understand why

  • June 16, 2011
  • 1 reply
  • 297 views

Hi,

I am completely new to actionscript although I do come from a OOP background. I have been asked to trial Flash and Actionscript 3 in particular - so I do come with a few preconceptions on how I expect actionscript to behave.

I have a problem I donot understand. I have created a new class and to test it I use a simple test harness in the form of a .fla file.

The issue I have is when I create a new instance of the class and assign it to a variable, subsequent background changes to that instance of the class are not 'picked-up' when referenced through the variable. I would expect that given instance of class could be assigned to many different variables and any updates using one variable would  accessible using any of the other variables.

My test class, XMLDataLoader, is coded as follows:

package  {   
   
    public class XMLDataLoader {
        import flash.events.*;
        import flash.net.*;
        import flash.utils.*
   
        private var _xmlData:XML;
        private var _xmlLoaded:Boolean;
       
        public function XMLDataLoader(pFileName:String){
            init(pFileName);
        }
       
        public function getXMLData():XML {
            return this._xmlData;
        }

        public function XMLLoaded():Boolean {
            return this._xmlLoaded;
        }

        private function init(pFileName:String):void
        {
           
            // Create the URLLoader instance to be able to load data
              var loader:URLLoader = new URLLoader( );
            var urlRequest:URLRequest = new URLRequest(pFileName);
            this._xmlLoaded = false;
           
           
            // Define the event handlers to listen for success and failure
               loader.addEventListener ( Event.COMPLETE, handleComplete );
           
            loader.load(urlRequest);

            function handleComplete ( e:Event ):void
            {
                trace ( "The data has successfully loaded" );
                this.xmlData = new XML(e.currentTarget.data);
                this._xmlLoaded = true;
            }
        }
    }
}

The class simply reads a text file and assigns its contents to the class variable _xmlData. The class variable _xmlLoaded identifies when the load is complete.

The test harness is a simple flash app consisting if a single timeline with 1 frame, 1 symbol on the stage and the following actionscript code:

import flash.events.TimerEvent;

var t:XMLDataLoader = new XMLDataLoader("Test Article.xml");

var xmlData:XML;

var waits:uint = 0;

wait(1);

function wait(ct:uint):void

{

    var TimerInstance:Timer = new Timer(500, 1);

    TimerInstance.addEventListener(TimerEvent.TIMER, TimerHandler);

    TimerInstance.start();

    function TimerHandler(event:TimerEvent):void

    {

       if(t.XMLLoaded()){

            xmlData = t.getXMLData();  

            trace(xmlData.toXMLString);

        } else {

            trace(ct + " - Waiting....");

            if (ct <= 10) {

                wait(++ct);

            }

        }

    }

}

The script defines a variable and assigns it a new instance of the XMLDataLoader. Originally, I immediately followed this with a trace statement but  insuffient time had elapsed to allow the load to complete, so I had to introduce a wait function to force a delay. This wait function is recursive. It checks to see if the load is complete. If it is, a trace statement will outoput the xml. If not the function will call itself again (upto 10 times)

The resultant output is as follows:

The data has successfully loaded
1 - Waiting....
2 - Waiting....
3 - Waiting....
4 - Waiting....
5 - Waiting....
6 - Waiting....
7 - Waiting....
8 - Waiting....
9 - Waiting....
10 - Waiting....

According to this output, the XML has been loaded before the end of the first timer cycle but this is not detected in the test harness. I have stepped through the code in debug mode and I have confirmed that the XML was loaded, and the class variables where correctly set, including _xmlLoaded = true. However, in the test harness the instance both class variables  are null. I don't believe this should be the case but obviously I must be wrong - can anyone explain where my logic is flawed. I did think scoping might be the problem but having tried a couple of modifications, I concluded that it wasn't.

Thanks in advance.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
June 16, 2011

never nest named functions.  unnest TimerHandler and retest.

(and, you should be using listener to determine when loading is complete.)