I’m having problems with asynchronous sequences on Flex/ActionScript.
Here is an example:
private function start():void{
_menu = new MyMenu();
_screen.addElement(_menu);
//Here, some Mouse Event Listener to Menu Click
}
Now, let’s assume that a click on the menu happened.
private function menuClick(event:Event):void{
removeMenu();
addMenu(event.SomethingPassedByTheClick);
}
Now, forget about the error on the event handler thing, let’s just think about the process, ok?
My problem is that sometimes addMenu() finishes before removeMenu(), which leads to errors.
The above script is just a logical representation of my problem, not a real script. To sunup, I need to be able to define that the method addMenu() HAS to wait for removeMenu() to be done before be called. Any thoughts?
Thanks for your attention.
Edit:
A more accurate example of my problem:
private function createComplete():void{
_screenArray = new Array(
new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
startUp();
}
private function startUp():void{
//Some mathematical calculations that changes a few 0 to 1's.
addNewComponent();
}
private function addNewComponent():void{
removeAllComponents();
//More calculus on the array in order to create a component in vague space.
addComponentOnCalculatedArea(x, y);
//here is my problem: Sometimes, add Method is called before the removeAllComponents, which causes the new added component be removed by the removeAllComponents() method.
}
Thanks everyone. I had the wrong premise. What was wrong whas my mathematics calcs, not the order of the methods being called. I noticed that by adding trace() in each method of the Script.
You should hinge on the component lifecycle to know when things are available. Don’t add the mouse event until the creationComplete event of the menu object is fired.
You will run into a lot of asynchronous issues if you don’t hinge on the events. This is the basis of asynchronous applications!
On top of that you should set flags when things are accomplished. If it is critical to never allow removeMenu to be evaluated before addMenu is finished you should be doing something like this:
Something along those lines.
Also you mentioned something about AddMenu waiting for RemoveMenu. While as3 is asynchronous it is also single threaded. You won’t have multiple functions evaluating at the same time. When RemoveMenus is running AddMenu won’t.
To make this clearer lets supposed we do this:
In this example it will ALWAYS print “first” then “second” if you call
doThingsno matter how longdoFirsttakes. IfdoFirsthas an infinite while loop nothing will break its flow control, it will stall the entire application.Now if
doFirst()dispatches some events and needs to do things asynchornously then the things it dispatches can happen afterdoSecondbut not the finishing of thedoFirstfunction. If thats the case you should register an event listener fordoSecondto fire after all the work indoFirstis completed.Like I also mentioned, you can set flags in the event handlers of the work you need to do and then you can invalidate the properties of your components. This will call
commitPropertiesand you can do the next items there.Check http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_3.html and http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/UIComponent.html#commitProperties() which are links explaining the asynchronous lifecycle of components and how to deal with setting properties asynchronously.