Suppose that you have an Object called Circus. This object is too complex (like a real circus). The follow init() function creates the circus it but in real programming world the Circus object is not prepared because the Circus must be render its components, update several graphics objects .
internal function init():void{
//create my.. circus…
myCircus=new Circus(“My X-circus");
//initialize
myCircus.x=10; myCircus.y=40;
//play
myCircus.play();
myCircus.cashier.addMoney(100.50); //this might be wrong, as the circus in not ready yet!
}
This problem occurred on simpler objects also… like the Flex’s Label. If on “applicationComplete” you apply a value to “text” property of a Label a text (i.e. “hellow world”) and right after you try to get the “width” of the Label object, you might be get WRONG value, a not real width; this because the Label object is not yet prepared.
Well, how to get that an object is completed? With Flex’s Label, the event you must Listen is the “FlexEvent.UPDATE_COMPLETE” and not other “UPDATE_COMPLETE” because Flex offers and other from other Eventxxx classes. Note that this is working in Flex 4.0.
So… the Circus object must supports and dispatch once upon a time the “FlexEvent.UPDATE_COMPLETE” to continue in safe way the calculations with its properties.
But, what happen if the Circus doesn’t supports this event? Unfortunately this is happening in real world with several 3rd party components / object.
Bellow there is a solution that is not so professional but it is the only chance to make it work! ;) What I do here is that I am working for a while with Event.ENTER_FRAME, with this way I give some time to then whole application and its running objects to complete uncompleted calculations.
//this is my own counter to count the passed time.
internal var myWaitCounter:int;
internal function init():void{
//create my.. circus…
myCircus=new Circus(“My X-circus");
//initialize
myCircus.x=10; myCircus.y=40;
//prepare and activate my listener pf ENTER_FRAME event
myWaitCounter=0;
addEventListener(Event.ENTER_FRAME,Handler_WaitForCompletion);
}
internal function Handler_WaitForCompletion (e:Event):void{
//add to my counter the time
myWaitCounter+=1000/ Object(FlexGlobals.topLevelApplication).stage.frameRate;
//if, 500ms (0,5sec) passed, then call the start() after remove then listener
//in general it is very important to remove the listeners where you don’t need any more
if (myWaitCounter >=500) {
removeEventListener(Event.ENTER_FRAME,Handler_WaitForCompletion);
start();
}
}
protected function start():void{
//now you are ready to play with you circus safely!
myCircus.play();
}
No comments:
Post a Comment