Question

I'm facing some problems in AS3. For example, I have two classes Car.as and ReadXML.as as follows:

Car.as

public class Car{

    public function get price():String{
      var priceXML:ReadXML = new ReadXML('price.xml');
      return priceXML.file;
    }
}

ReadXML.as

public class ReadXML{
    public var file:XML;
    public var loader:URLLoader;

    public function ReadXML(fileName:String):void{
        loader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, _loadComplete);
        loader.load(new URLRequest(fileName));
    }

    private function _loadComplete(e:Event):void{
        file = new XML(loader.data);

        loader.removeEventListener(Event.COMPLETE, _loadComplete);
    }
}

But when I try to access the price property,

var carObj:car = new Car();
trace(carObj.price)

it returns null which I presume is because the URLLoader hasn't been finished. So what is an alternative or solution to this? Thanks!

Was it helpful?

Solution

I think loading an XML file every time you want to check an instance's 'price' property is the problem.
I'd recommend loading the XML file as part of your application's initialisation, and including the _loadComplete function as part of that initialisation; When the XML is loaded, your application can then continue and instantiate as many 'new Car()'s as it fancies - no delay will be required if the XML is pre-loaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top