Event Controller in Action

Event Controller in Action

Event Controller is a very basic yet extremely powerful library. You can easily update any current project in minutes to utilize the Event Controller library. From beginners to advanced developers, everyone should find something for them. Below you will find a few examples of the simplicity of EC (Event Controller) and also how to empower yourself with it. We recommend taking the time to understand each example.

Notes: Event Controller is not a new event module but its goal is to monitor/catagorizing events to simplify how you controll events by adding grouping & dynamic grouping posiblities to cut down managment tasks. These events are still being made through the native Flash event model.

Links: basics | intro to clusters | regexp feature | event logging | local event control

The Basics

Adding and removing event with EventController. Below you will see the basic long hand way to add events to event controller. You will see how familiar it is very quickly. Basically all that has been done is moving the target inside the method and calling it through Event Controller so you can now use the new features.

view plaincopy to clipboardprint?

  1. import _as.fla.events.EventController; 
  2. var mc:MovieClip = new MovieClip(); 
  3. //basic add event
  4. //will return a Boolean to indicate if event was added
  5. EventController.addEventListener(mc, Event.ENTER_FRAME, loop, false, 0, true);  
  6. function loop(e:Event):void{} 
  7. //basic remove event
  8. //will also return an Array of the event you can store and add events back easily using the ".addGroup" method
  9. EventController.removeEventListener(mc, Event.ENTER_FRAME, loop, false);  

Notes:The EventController returns Boolean values to let you know if things worked out as you planned. For example when adding an event that is already on the system the EventController will return false as it will not allow you to add the same exact event twice(as its pointless and a big place for memory leaks to creep up at). When you are removing an event it will let you know if that event was running or not.

Ok now that you have the hang of that let's do it a little faster and maybe less code too. We are just going to shorten the EventController.addEventListener syntax to EC.add. Also changing EventController.removeEventListener to the much shorter EC.rem. One thing to know is you can use the long and short methods the exact same way with all features in Event Controller.

view plaincopy to clipboardprint?

  1. import _as.fla.events.EC; 
  2. var mc:MovieClip = new MovieClip(); 
  3. //basic add event
  4. //the default false,0,false will be added to the function if not explicitly requested
  5. EC.add(mc, Event.ENTER_FRAME, loop);  
  6. function loop(e:Event):void{} 
  7. //basic remove event
  8. // will remove all events associated to the EventDispatcher(in this case a movieclip)
  9. // and return an Array listing out all of the events that have been removed, if you save this array you can then send it back to ".addGroup" to reactivate them.
  10. EC.remove(mc);  

Easy enough huh? So now what you ask what else?
Back To Top

Introducing Event Clusters

Event clusters are where you will find the reason Event Controller was created. It's a simple and easy way for you to group your events together. This is going to save you hours of tracking down those pesky old events that may have gotten lost in your app.

view plaincopy to clipboardprint?

  1. import _as.fla.events.EC; 
  2. var btn:MovieClip = new MovieClip(); 
  3. //basic add event
  4. EC.add(btn, Event.ENTER_FRAME, loop, "button");  
  5. EC.add(btn, MouseEvent.CLICK, onClick, "button"); 
  6. EC.add(btn, MouseEvent.ROLL_OVER, onOver, "button"); 
  7. EC.add(btn, MouseEvent.ROLL_OUT, onOut, "button"); 
  8. function loop(e:Event):void{} 
  9. function onClick(e:MouseEvent):void{} 
  10. function onOver(e:MouseEvent):void{} 
  11. function onOut(e:MouseEvent):void{} 
  12. // yes you can still change your useCapture,priority,useWeakReference attributes
  13. // using the same normal flow but pushing the cluster name to the last param:
  14. //EC.add(btn, MouseEvent.ROLL_OUT, onOut, false, 0, true, "button"); // all the same
  15. //remove a cluster of events
  16. EC.remove("button");  
  17. //or remove it by sending the object to be removed
  18. EC.remove(btn); 

So what do you think? So far so good? Clusters are where the power of using EventController begins to unfold. By creating naming connvections around your clusters you can really begin to take power over your application. You can remove any and all events associated with a cluster or type of cluster very quickly.
Back To Top

Clusters and all the power of RegExp

Now I think you are starting to see what's possible with Event Controller and clusters. Next we are going to introduce another powerful feature. Under the hood of Event Controller we have added you the ability to remove events with RegExp. It's now all up to you to send in any REgExp you desire to help clean up you events. Take a look below at the most basic example.

view plaincopy to clipboardprint?

  1. import _as.fla.events.EC; 
  2. var btn:MovieClip = new MovieClip(); 
  3. //basic add event
  4. EC.add(btn, Event.ENTER_FRAME, loop, "button_loop"); 
  5. EC.add(btn, MouseEvent.CLICK, onClick, "button_mouse"); 
  6. EC.add(btn, MouseEvent.ROLL_OVER, onOver, "button_mouse"); 
  7. EC.add(btn, MouseEvent.ROLL_OUT, onOut, "button_mouse"); 
  8. function loop(e:Event):void{} 
  9. function onClick(e:MouseEvent):void{} 
  10. function onOver(e:MouseEvent):void{} 
  11. function onOut(e:MouseEvent):void{} 
  12. //remove cluster on button that work only with the mouse
  13. EC.remove(/_mouse/); 
  14. // REMINDER: With great power comes great responsibility. Using the regex powers has a global effect to the current Event Controller
  15. // Beware that you can easily remove events in the Event Controller you may or may not of meant to remove.
  16. // We HIGHLY recommend using a unique id before each cluster if needed. (ie: uid + "button_mouse")

It's that easy. Event Controller is a great way for you to manage and take back control of your events. Once you understand the power of EC you will start writing much more simple event handling methods. As you saw in the RegExp portion you can now pick and choose in a very clean way which events you'd like to sort.
Back To Top

Logging All Your Events

As we said before we all know that you can easily lose events in you app. Well now you don't have to worry about any longer. EC.log() is a method that will save you hours of headaches tracking down those events. You can find events with RegExp even as long as you follow your cluster ids. Examples below.

view plaincopy to clipboardprint?

  1. import _as.fla.events.EC; 
  2. //plugin required for logging. Check docs for other popular built in flash loggers.
  3. import _as.fla.events.log.ClassicLog; 
  4. EC.plug(ClassicLog);  
  5. var btn:MovieClip = new MovieClip(); 
  6. //basic add event
  7. EC.add(btn, Event.ENTER_FRAME, loop, "button_loop"); 
  8. EC.add(btn, MouseEvent.CLICK, onClick, "button_mouse"); 
  9. EC.add(btn, MouseEvent.ROLL_OVER, onOver, "button_mouse"); 
  10. EC.add(btn, MouseEvent.ROLL_OUT, onOut, "button_mouse"); 
  11. function loop(e:Event):void{} 
  12. function onClick(e:MouseEvent):void{} 
  13. function onOver(e:MouseEvent):void{} 
  14. function onOut(e:MouseEvent):void{} 
  15. //output all events in my application
  16. EC.log(); 
  17. //or
  18. EC.log(btn); 
  19. //or
  20. EC.log(/button/) 
  21. /* output
  22. =======================
  23. =======================
  24. clusters-----------------------
  25. button_loop
  26.             obj         name            type        listener                useCapture
  27.     [object MovieClip]      instance1       enterFrame      function Function() {}      false
  28. button_mouse
  29.             obj         name            type        listener                useCapture
  30.     [object MovieClip]      instance1       click       function Function() {}      false
  31.     [object MovieClip]      instance1       rollOver        function Function() {}      false
  32.     [object MovieClip]      instance1       rollOut     function Function() {}      false
  33. -----------------------
  34. All objects that are tracked by the EventController:
  35.             instance1   [object MovieClip]
  36. =======================
  37. =======================
  38. */

In the log method you can use all the same power of RegExp that you could use anywhere for finding a cluster easily or viewing all events in the current scope of your Event Controller.

All new logger plugins

As of release 1.3 and up we have added support for popular Flash loggers. First was the ClassicLog which you have seen above but we have also added in support for loggers like Alcon, Arthropod and Monster Debugger. This is to help out anyone already locked into a debugger.

Back To Top

Introducing LEC

I know some of you are now asking "what can you possibly add, we can do everything already". You might notice that this supper powers might become problematic if your trying to limit this accesses. This is where the LEC comes in its a location EC. In other words you can run local event managers (as many as you want) to control responsibilities over your events. This enables you to pick when or where you want to keep your secrets, I mean events, and still control them. The three methods add/remove/log do the exact same thing on all three classes only when working on the LEC objects the scope is local while working on the EC/EventController the scope is global.

view plaincopy to clipboardprint?

  1. import _as.fla.events.LEC; 
  2. var btn:MovieClip = new MovieClip(); 
  3. var lec:LEC = new LEC(); 
  4. //basic add event
  5. lec.add(btn, Event.ENTER_FRAME, loop, "button");  
  6. lec.add(btn, MouseEvent.CLICK, onClick, "button"); 
  7. lec.add(btn, MouseEvent.ROLL_OVER, onOver, "button"); 
  8. lec.add(btn, MouseEvent.ROLL_OUT, onOut, "button"); 
  9. function loop(e:Event):void{} 
  10. function onClick(e:MouseEvent):void{} 
  11. function onOver(e:MouseEvent):void{} 
  12. function onOut(e:MouseEvent):void{} 
  13. // yes you can still change your useCapture,priority,useWeakReference attributes
  14. // using the same normal flow but pushing the cluster name to the last param:
  15. //lec.add(btn, MouseEvent.ROLL_OUT, onOut, false, 0, true, "button"); // all the same
  16. //remove a cluster of events
  17. lec.remove("button");  
  18. //or remove it by sending the object to be removed
  19. lec.remove(btn); 
  20. //or you can regexp it :
  21. lec.remove(/button/); 

So what do you think? So far so good? Clusters are where the power of using EventController begins to unfold. By creating naming connvections around your clusters you can really begin to take power over your application. You can remove any and all events associated with a cluster or type of cluster very quickly.
Back To Top

posted on 2010-10-30 09:22  Morris  阅读(222)  评论(0编辑  收藏  举报

导航