66
關於Event這回事 photo by Luke Peterson Photography there's something about Event

There is something about Event

Embed Size (px)

Citation preview

  • 1.Event theres something about Eventphoto by Luke Peterson Photography

2. a.k.a Eddie or Aquarianboy Live and work in Taipei, Taiwan. Serving in my own little tiny company. Flash / AS3 / Ruby / Rails / Python programming for living. Mac OS user, Objective-C for personal inerests. Technical Education and Consulant. PTT Flash BM (since 2007/4). Ruby Certied Programmer (Since 2012/1). Adobe Certied Flash Developer (Since 2006/7). Linux Professional Institue Certication (Since 2005/3). photo by Eddie 3. why 4. ()MC?how to interact with parent (or child) movieclips? 5. ? how to know which button was clicked? 6. swf?why swf le can not get the right response from the internet? 7. ?how to know if image les were downloaded completly? 8. event 9. btn.onRelease = function() { _root.hero_mc.gotoAndPlay(10);} 10. mc.onRelease = function() {_parent._parent.mc.title_txt.text = "this is not good!";} 11. loose coupling 12. listener 13. listenerfunctionlistener is just a function 14. function click_handler(e:MouseEvent):void { trace("hello, everyone!");} 15. register 16. addEventListener 17. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 18. mc.addEventListener(MouseEvent.CLICK, click_handler); 19. event name 20. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 21. event name is actually just plain string 22. trace(MouseEvent.CLICK);// "click"trace(IOErrorEvent.IO_ERROR); // "ioError" 23. mc.addEventListener(MouseEvent.CLICK, click_handler); 24. mc.addEventListener("click", click_handler); 25. remove 26. mc.removeEventListener(MouseEvent.CLICK, click_handler); 27. remember to remove useless listener 28. save the world! 29. ow 30. phases 31. capture, bubble, and the target 32. photo by Luke Peterson Photography 33. reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html 34. bubbling 35. dispatchEvent(new Event(Event.COMPLETE, bubbles:Boolean,cancelable:Boolean)); 36. capture 37. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 38. mc.addEventListener(MouseEvent.CLICK, click_handler, true); 39. reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html 40. reference 41. addEventListener("click", function(e) { // code }); 42. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 43. addEventListener("click", function(e) { // code }, false, 0, true); 44. Priority 45. function handler_a(e:Event):void { trace("hello, A");}function handler_b(e:Event):void { trace("hello, B");}function handler_c(e:Event):void { trace("hello, C");}mc.addEventListener(MouseEvent.CLICK, handler_a);mc.addEventListener(MouseEvent.CLICK, handler_b);mc.addEventListener(MouseEvent.CLICK, handler_c); 46. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 47. mc.addEventListener(MouseEvent.CLICK, handler_a, false, 1);mc.addEventListener(MouseEvent.CLICK, handler_b, false, 3);mc.addEventListener(MouseEvent.CLICK, handler_c, false, 2); 48. the target 49. target v.s. currentTarget 50. dispatch 51. dispatchEvent(new MouseEvent(MouseEvent.CLICK)); 52. custom events 53. package digik.events{import ash.events.Event;public class LotteryEvent extends Event {public static const REMOVE_BANNER:String = "remove the banner";public var dataObject:Object; public function LotteryEvent(type:String , dataObj:Object = null,bubbles:Boolean = true, cancelable:Boolean = false, ...rest):void {super(type, bubbles, cancelable);dataObject = dataObj;} override public function clone():Event {return new LotteryEvent(type, dataObject, bubbles, cancelable);}}} 54. my_button.addEventListener(MouseEvent.CLICK, click_handler);function click_handler(e:MouseEvent):void { dispatchEvent(new LotteryEvent(LotteryEvent.REMOVE_BANNER, {id: 2});} 55. addEventListener(LotteryEvent.REMOVE_BANNER, banner_removed); 56. function banner_removed(e:LotteryEvent):void { trace(e.dataObject.id); var the_banner:Banner = this[banner_" + e.dataObject.id] as Banner; if (contains(the_banner)) { removeChild(the_banner); }} 57. conclusion 58. addEventListener(MouseEvent.CLICK, click_handler); 59. public function addEventListener(type:String, listener:Function,useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void 60. reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html 61. remember to remove useless listener 62. public function Lottery():void {if (stage) init();else addEventListener(Event.ADDED_TO_STAGE, added_to_stage);} private function added_to_stage(e:Event):void {removeEventListener(Event.ADDED_TO_STAGE, added_to_stage);init();}private function init():void { // do something here} 63. signal 64. end 65. Conacts WebsieBlog http://www.eddie.com.tw http://blog.eddie.com.twPlurkhttp://www.plurk.com/aquarianboyFacebook http://www.facebook.com/eddiekaoGoogle Plus http://www.eddie.com.tw/+Twiterhttps://twiter.com/#!/[email protected] +886-928-617-687photo by Eddie