Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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 inerests. Technical Education and Consulant. PTT Flash BM (since 2007/4). Ruby Certified Programmer (Since 2012/1). Adobe Certified Flash Developer (Since 2006/7). Linux Professional Institue Certification (Since 2005/3). 高見見龍龍 photo by Eddie

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

源起 why

Slide 5

Slide 5 text

如何與上(下)層的MC互動? how to interact with parent (or child) movieclips?

Slide 6

Slide 6 text

想知道哪個按鈕被按了? how to know which button was clicked?

Slide 7

Slide 7 text

為什麼swf讀不到網路資料? why swf file can not get the right response from the internet?

Slide 8

Slide 8 text

如何知道圖片已下載完成? how to know if image files were downloaded completly?

Slide 9

Slide 9 text

事件 event

Slide 10

Slide 10 text

btn.onRelease = function() { _root.hero_mc.gotoAndPlay(10); }

Slide 11

Slide 11 text

mc.onRelease = function() { _parent._parent.mc.title_txt.text = "this is not good!"; }

Slide 12

Slide 12 text

鬆綁 loose coupling

Slide 13

Slide 13 text

聆聽 listener

Slide 14

Slide 14 text

listener只是個function listener is just a function

Slide 15

Slide 15 text

function click_handler(e:MouseEvent):void { trace("hello, everyone!"); }

Slide 16

Slide 16 text

註冊 register

Slide 17

Slide 17 text

addEventListener

Slide 18

Slide 18 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 19

Slide 19 text

mc.addEventListener(MouseEvent.CLICK, click_handler);

Slide 20

Slide 20 text

名稱 event name

Slide 21

Slide 21 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 22

Slide 22 text

事件名稱其實只是字串 event name is actually just plain string

Slide 23

Slide 23 text

trace(MouseEvent.CLICK); trace(IOErrorEvent.IO_ERROR); // "click" // "ioError"

Slide 24

Slide 24 text

mc.addEventListener(MouseEvent.CLICK, click_handler);

Slide 25

Slide 25 text

mc.addEventListener("click", click_handler);

Slide 26

Slide 26 text

移除 remove

Slide 27

Slide 27 text

mc.removeEventListener(MouseEvent.CLICK, click_handler);

Slide 28

Slide 28 text

沒用到的記得要收掉 remember to remove useless listener

Slide 29

Slide 29 text

隨手做環保 save the world!

Slide 30

Slide 30 text

流程 flow

Slide 31

Slide 31 text

階段 phases

Slide 32

Slide 32 text

capture, bubble, and the target

Slide 33

Slide 33 text

photo by Luke Peterson Photography

Slide 34

Slide 34 text

reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html

Slide 35

Slide 35 text

冒泡泡 bubbling

Slide 36

Slide 36 text

dispatchEvent(new Event(Event.COMPLETE, bubbles:Boolean, cancelable:Boolean));

Slide 37

Slide 37 text

捕捉 capture

Slide 38

Slide 38 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 39

Slide 39 text

mc.addEventListener(MouseEvent.CLICK, click_handler, true);

Slide 40

Slide 40 text

reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html

Slide 41

Slide 41 text

參考 reference

Slide 42

Slide 42 text

addEventListener("click", function(e) { // code });

Slide 43

Slide 43 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 44

Slide 44 text

addEventListener("click", function(e) { // code }, false, 0, true);

Slide 45

Slide 45 text

順序 Priority

Slide 46

Slide 46 text

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);

Slide 47

Slide 47 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 48

Slide 48 text

mc.addEventListener(MouseEvent.CLICK, handler_a, false, 1); mc.addEventListener(MouseEvent.CLICK, handler_b, false, 3); mc.addEventListener(MouseEvent.CLICK, handler_c, false, 2);

Slide 49

Slide 49 text

目標 the target

Slide 50

Slide 50 text

target v.s. currentTarget

Slide 51

Slide 51 text

發布 dispatch

Slide 52

Slide 52 text

dispatchEvent(new MouseEvent(MouseEvent.CLICK));

Slide 53

Slide 53 text

客制化 custom events

Slide 54

Slide 54 text

package digik.events { import flash.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); } } }

Slide 55

Slide 55 text

my_button.addEventListener(MouseEvent.CLICK, click_handler); function click_handler(e:MouseEvent):void { dispatchEvent(new LotteryEvent(LotteryEvent.REMOVE_BANNER, {id: 2}); }

Slide 56

Slide 56 text

addEventListener(LotteryEvent.REMOVE_BANNER, banner_removed);

Slide 57

Slide 57 text

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); } }

Slide 58

Slide 58 text

小結 conclusion

Slide 59

Slide 59 text

addEventListener(MouseEvent.CLICK, click_handler);

Slide 60

Slide 60 text

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Slide 61

Slide 61 text

reference: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html

Slide 62

Slide 62 text

用完記得要收 remember to remove useless listener

Slide 63

Slide 63 text

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 }

Slide 64

Slide 64 text

訊號 signal

Slide 65

Slide 65 text

結束 end

Slide 66

Slide 66 text

高見見龍龍 Conacts photo by Eddie Websie Blog Plurk Facebook Google Plus Twiter Email Mobile http://www.eddie.com.tw http://blog.eddie.com.tw http://www.plurk.com/aquarianboy http://www.facebook.com/eddiekao http://www.eddie.com.tw/+ https://twiter.com/#!/eddiekao [email protected] +886-928-617-687