11.23
Is now a good time to start using Event.ADDED_TO_STAGE vs. your class constructor for initialaztion?
I have always had a habit of doing all my init stuff in the class constructor as do most other people. But today I found a GOTCHA in Flash CS4 when publishing an AIR file. I was trying to use a ProgressBar and set it’s value manually. I could not see the bar change it’s value even though there was no errors. Nothing I did made it work. This was a problem since there was no errors letting me know that there was a problem, the progress bar just did not work and that’s all I had to go off of. Later I tried to place my document class code on the timeline which made the progress bar work, leaving me even more confused and fustrated as to the problem.
In the End I found this had to do with the document class trying to set properties of the ProgressBar before the document class had a chance to be added to the stage.
Aside from my story, this may or may not ever be a problem for you , but it begs the question should this be a standered and would you follow it?






I do this regularly, and use constructor for init.
Flash Develop already have template for this
I have been leaning to using the constructor to set properties (not involving a stage) and having most classes have a start() function
Would you guys say it’s a standard already then? The only reason I ask is until I seen it in flashDevelop I never seen it any where in the community for flash. And you guys are the first I have ever seen say to do it this way.
Maybe I’m just not hanging out with the cool kids, lol.
I always do this:
public function Constructor(stageInit:Boolean = false)
{
if (stageInit) addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
public function init(event:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
so if the class gets added to the display list, it’s possible enable init. Or, you call call the init method manually. Removing the listener prevents calling init a few times when you nest the objects.