I’m seeing some strange behavior with respect to interactions between my preloader and main application classes (all AS3 / Flash CS4). Roughly speaking, here’s the flow of events:
- Preloader.swf loads two things: main.swf, which is the main app, and assets for a custom object consisting of text and images, which are assembled into the object by the preloader from various URLs.
- When load finishes, Preloader adds main.swf as a child. Preloader then calls
init(myCustomObject)
on main.swf, where myCustomObject is a reference to the object assembled by the preloader during step 1 and
public function init(customObject:CustomObject):void)
is a method signature in Main.as. (Preload.as casts Main to an object of type * so as to be able to invoke arbitrary functions without fear of compile-time errors.)
- Main.as is actually a container for the application itself, so it instantiates a
new Application(customObject);
passing along a reference to the customObject assembled by the preloader, and adds that as a child.
I’ve installed thunderbolt so I can log messages as the application runs, and here’s what I’ve determined is happening. The instantiation of the Application object in step 3 is what’s causing trouble; for some reason, the statement
myMainApplication = new Application(customobj); in Main.as is throwing a lovely #1009 error, which usually indicates a null pointer reference or something similar.
The strange thing is that I’ve added some logging to Application.as, and it seems to be receiving the reference to customObject without a problem; calling toString() on the customObject in Application‘s constructor returns exactly the expected data.
In other words, the statement myMainApplication = new Application(customobj); in Main.as seems to be succeeding and failing at the same time. What gives?
The reason that
myMainApplication = new Application(customobj);seemed to be succeeding and failing at the same time is that I didn’t completely understand the way thetry/catchblocks operate in AS3. There was a null pointer exception being thrown in a subroutine toApplication‘s constructor, occurring after the code in whichApplicationchecks to ensure it’s receiving a reference to customobj. This error was being caught by thetry/catchblock surrounding the instantation ofApplicationinMain, as it was the nearest enclosing error-checking code.Hopefully my mistake will save someone else for making a similar one!