开发者

In what order do events fire when added to buttons, in Flash?

开发者 https://www.devze.com 2023-04-09 20:50 出处:网络
If I do this: myButton.addEventListener(MouseEvent.CLICK, doThingA); myButton.addEventListener(MouseEvent.CLICK, doThingB);

If I do this:

    myButton.addEventListener(MouseEvent.CLICK, doThingA);
    myButton.addEventListener(MouseEvent.CLICK, doThingB);

Is there any guarantee that when the user clicks the button the events will 开发者_如何学Gobe fired in some sequence, and if so, what's the order? Or does the first event just get deleted?


They are called in the order registered, so in your example doThingA will be called before doThingB as long as they have the same priority.

To alter which is triggered first then simply add a seperate priority for each listener. The listener with the highest priority will be triggered first, then the one with the lower priority.

myButton.addEventListener(MouseEvent.CLICK, doThingA, false, 0); // second
myButton.addEventListener(MouseEvent.CLICK, doThingB, false, 1); // first

Hope that helps.


They both have a default priority of zero (priority:int = 0 from arguments given to addEventListener) so the sequence is the order they are added.

To change the order after this you will need to re-register the listener.

Another way would be to make helper functions that push the multiple listeners to a list and give each one a name. Then add the events within that helper function.

myButton.addEventListenerHelper(TypeA, MouseEvent.CLICK, doThingA, false, 0); 
myButton.addEventListenerHelper(TypeB, MouseEvent.CLICK, doThingB, false, 1); 
// And then remove by making some helper function to iterate the list for the 
// given listener
myButton.removeEventListenerHelper(TypeA, MouseEvent.CLICK);
0

精彩评论

暂无评论...
验证码 换一张
取 消