Actionscript 2.0: Where can I place a class instance so that I can access it from other classes?

StackOverflow https://stackoverflow.com/questions/13858321

  •  07-12-2021
  •  | 
  •  

Question

I currently have three classes, Main, Externals, and Page.

The Main class creates an instance of the Externals class, which loads XML, and, once the XML is loaded, that Externals instance also creates an instance of the Page class which will then use information from the Externals class to populate itself.

I originally wanted Externals to be a static class, then I found out that there are no static classes in Actionscript 2.0, only static methods.

Then I declared all my Externals methods as static, but found that when I tried to access them, the constructor was not run, and the XML was not loaded and parsed. I thought about rewriting the methods to load and parse the XML every time, but realized that A: that would be really wasteful of network resources; and B: since the network is slower than the processor, I would run into timing issues, as the code would try to access data that wasn't yet loaded.

So then I decided to revert everything to instance methods and create an instance in the Main class that would run its constructor, load itself up with XML, parse everything into neat getters, and only then, create an instance of Page, which creates the page that will consume the XML data.

But I was (and still am) left with a conundrum: I can only create the instance of the Page class from inside the Externals class, because it can only start AFTER the XML data has already been loaded, but I can't access the data that I've gathered into my instance of Externals from anywhere but the Main class. The only way for me to get at it in my instance of Page is to create a new instance, but this is a very bad idea for two reasons: 1: the above-mentioned timing issues; and 2: the Externals class creates a new Page when it finishes loading the XML, so it ends up making a new Page instance, which makes a new Externals instance, which makes a new Page instance, and so on.

I tried placing the instance of Externals on the movie's root like this:

_root.externals:Externals = new Externals();

but I couldn't access it there either.

Now I'm a bit stumped. I really don't understand how this can be done, but I know that there has to be a way. Does anybody see what I am doing wrong here?

No correct solution

OTHER TIPS

I suppose you can try Singleton design pattern: http://voices.yahoo.com/building-singleton-actionscript-2-as2-4849702.html

I am guessing the Externals class is a singleton? You could just pass in a reference of the one Externals object to all your other objects.

Or another way (though I don't always recommend it, but seems like what you are leaning to do) is to create the object at the root level like normal, then access it in your other objects by going: _root.externalsObject

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