PDA

View Full Version : preloader problem



flappypenguin
October 14th, 2009, 10:39 AM
Hey all,

Im using the preloader technique as per Lee Brimelow's external preloader solution:

http://www.zedia.net/2008/the-right-way-to-do-a-preloader-in-as3/

However, when the preloader is finished i get the error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main()

And through a process of elimination, i have found that it is where i have any reference to "stage." in my constructor (Main), if i remove these the swf loads as expected...

what should "stage." become now im using a preloader, i have tried "parent.stage." but that throws the same error.

Any help/advice is appreciated.

flappypenguin
October 14th, 2009, 10:47 AM
nevermind, fixed it by taking all my code out the constructor and into a new function, and added:


addEventListener(Event.ADDED_TO_STAGE, doSomething, false, 0, true);

to my constructor, basically the stage didnt exist so...

HotN
October 14th, 2009, 10:49 AM
I typically use a function like this:

public function findStage(addedObj:DisplayObject):Stage {
if (addedObj.stage) {
return addedObj.stage;
}
else {
var target:DisplayObjectContainer = addedObj.parent;
while (!target.stage) {
target = target.parent;
}
return target.stage;
}
}


You pass a display object in from the scope where you would need stage access. One precondition is the display object you pass in must already be added to the display list.

If your loaded swf needs to know where stage is in order to add other display objects, add an Event.ADDED_TO_STAGE listener inside the constructor. In the listener function, then call findStage(this). That will return the instance of Stage, which you can store for later use.