views:

372

answers:

5

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:

  1. 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.

  2. 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.)

  3. 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?

A: 

Make sure your Global Security Settings in Flash Player allows local access to the directory.

Try this. Go to Publish Settings - Local playback security - choose "access network only." Flash CS4 default is "Access local files only" which it may not like. I hope this is useful.

See: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/system/Security.html

VideoDnd
A: 

I think we'll need to see more code, but to start: "customobj" ... where are you defining that? You refer to "customObject" everywhere else. Is that a typo? If that exact statment is throwing the error then "customobj" is most likely null.

Typeoneerror
+1  A: 

My guess is that your casting accross application domains, so although the types are the same files, in the memory of each application domain they are 2 separate concrete types. There are many ways to get around this, possibly starting with loading into a sibling application domain instead of a child domain, or not bothering with the cast and explicitly calling the function in question on the untyped object.

WeLoveAppDomain should be of some assistance if it is in fact this issue. Can you put together the smallest possible example to demonstrate this failure for us to de-construct?

JTtheGeek
+1: I have a feeling AppDomain is what's needed.
Heath Hunnicutt
A: 

Are you using FlexBuilder/FlashDevelop or the flash IDE? Also, how are you setting up your Preloader?

If you build your app with FlexBuilder/FlashDevelop, did you specify the frame start in your compiler options?

goliatone
A: 

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 the try/catch blocks operate in AS3. There was a null pointer exception being thrown in a subroutine to Application's constructor, occurring after the code in which Application checks to ensure it's receiving a reference to customobj. This error was being caught by the try/catch block surrounding the instantation of Application in Main, as it was the nearest enclosing error-checking code.

Hopefully my mistake will save someone else for making a similar one!

justinbach