Quite a lot lately I have been using the ADDED_TO_STAGE event listener in the main document class of my projects, for a couple of reasons:
The best method of pre-loading an application seems to be creating a preloader swf and loading the main swf into it, this ensures that the complete application is loaded first before it plays, unlike the old as2 days or the way some people do it which is to pre-load on the time line which of course doesn't always guarantee that everything is loaded. Anyway, if the ADDED_TO_STAGE listener isn't used, the loaded swf plays before it has chance to recognise the stage and results in an error, so the listener gives the swf a little time to wait before it is added to the stage before it does anything. This not only works nice and smoothly but also guarantees that if the swf is ever loaded again into another application or swf it is able to play properly without any error.
It also gives the swf a chance to recognise the stage before any of the assets make a call to the stage, which of course would result in an error. It is very important that no assets are called as new or added to the stage until the listener is completed or again another error would ensue.
It is just a small amount of code which once added can save you lots of head aches....at least it did me.....
-
public function Main():void{
-
addEventListener(Event.ADDED_TO_STAGE, init);
-
}
-
private function init(e:Event):void{

Posted in
Tags: 



![Validate my RSS feed [Valid RSS]](http://www.ultravisual.co.uk/blog/images/valid-rss.png)


JGKM2W Thanks for good post
But as I have been finding out – what about when added to stage doesnt work! Ie pre flash player FP 9.0.28.0 and in IE 6 – http://blog.hydrotik.com/2007/11/19/added_to_stage-ie6-debugging/
Gotta say that I haven’t come across this problem my self……Just typical of IE to put a spanner in the works though eh?!
This is great – came up in a google search for ADDED_TO_STAGE, it addresses exactly the problem I was having (loading in a main swf from a preloader).
In the init() function it would be wise to also remove this listener with:
removeEventListener(Event.ADDED_TO_STAGE, init);