Adobe ActionScript Article: Introduction to event handling in ActionScript 3.0
Event Object 由外傳至內(capture phase), 達到標的物件(target phase)後, 再往外傳(bubbling phase)
addEventListener() 的第三個參數 (useCapture) 就是用來決定,
偵聽器是否在 capture phase 時偵聽事件並觸發
useCapture 預設值為 false, 因此事件在 target phase 或 bubbling phase 可以被偵聽。
以下是個說明的例子:
var m1:MovieClip = new MovieClip();
m1.graphics.beginFill(0xFF9900);
m1.graphics.drawRect(100, 100, 200, 200);
m1.graphics.endFill();
m1.name = 'm1';
var m2:MovieClip = new MovieClip();
m2.graphics.beginFill(0x99FF00);
m2.graphics.drawRect(140, 140, 120, 120);
m2.graphics.endFill();
m2.name = 'm2';
addChild(m1);
m1.addChild(m2);
function mHandler(e:MouseEvent) {
switch (e.eventPhase) {
case EventPhase.CAPTURING_PHASE :
trace('capture');
break;
case EventPhase.AT_TARGET :
trace('at target');
break;
case EventPhase.BUBBLING_PHASE :
trace('bubbling');
}
trace('currentTarget: ' +e.currentTarget.name + e.currentTarget);
trace('target: ' + e.target.name + '\n');
}
stage.addEventListener(MouseEvent.CLICK, mHandler);
root.addEventListener(MouseEvent.CLICK, mHandler);
m1.addEventListener(MouseEvent.CLICK, mHandler);
m1.addEventListener(MouseEvent.CLICK, mHandler); // 重複註冊偵聽器
m2.addEventListener(MouseEvent.CLICK, mHandler);
stage.addEventListener(MouseEvent.CLICK, mHandler, true);
root.addEventListener(MouseEvent.CLICK, mHandler, true);
m1.addEventListener(MouseEvent.CLICK, mHandler, true);
m2.addEventListener(MouseEvent.CLICK, mHandler, true); // 這一行沒有作用
用滑鼠擊點 m2 後的結果:
capture
currentTarget: null[object Stage]
target: m2
capture
currentTarget: root1[object MainTimeline]
target: m2
capture
currentTarget: m1[object MovieClip]
target: m2
at target
currentTarget: m2[object MovieClip]
target: m2
bubbling
currentTarget: m1[object MovieClip]
target: m2
bubbling
currentTarget: root1[object MainTimeline]
target: m2
bubbling
currentTarget: null[object Stage]
target: m2
沒有留言:
張貼留言