📜 ⬆️ ⬇️

Another way to manage event listeners

Events rarely occur one by one as is usually written in the examples. In life, we deal with sequences of events (for example, connection, authorization, call) and at each stage other events may occur (call cancellation, connection disconnection).

Try to say with words what should be happening, you can hear a lot "if we received an event then ..., otherwise if we clicked here, and if we time out, then." Here I describe my attempt to program events in the style of if-else.


//  event listener- beginGroup(); //  btn_shoot_mc.addEventListener("click",function ...); // listener     ,   handler // "none" -    // "self" -   listener // "group" -   listener-   wait(btn_shoot_mc,"click",function(e:Object){ // return "group"; }); timeout(2.0,function(e:Object){ //,   return "group"; }); timer(0.5,function(e:Object){ //    return "none"; }); endGroup(); 

')
The capabilities of ActionScript do not allow using the usual if-else syntax, but calls to wait, timeout, timer inside beginGroup and endGroup essentially fulfill this role.

Another example: context menu
 import com.imtqy.comvdin.eventtree.*; // items -  MovieClip-       wait(items,"click",function(e:Object){ var item:Sprite = e.currentTarget; //  var h:Function = arguments.callee; addChild(menu); menu.x = item.x; menu.y = item.y; //    nextFrame(function(e:Object){ //     beginGroup, endGroup //       wait(menu,"click",function(e:Object){ //  -  menu.parent.removeChild(menu); //   wait(items,"click",h); return "group"; }); //   stage    MovieClip   wait([stage,item],"click",function(e:Object){ menu.parent.removeChild(menu); //  wait(items,"click",h); return "group"; }); //  item ,     wait(items,"click",function(e:Object){ if(e.currentTarget == item) return "none"; h(e); return "group"; }); }); return "self"; }); 

Source: https://habr.com/ru/post/111089/


All Articles