分享

事件流机制的3个阶段

 灵岩侠客 2011-10-16

事件流机制的3个阶段来源:我要学flash网 | 作者:admin | 发表时间:2011-07-02 | 点击:162  次根据个人的学习体会,我做了大致以下总结。事件流机制主要分为3个阶段:捕获阶段、目标阶段、冒泡阶段。

1、捕获阶段:当发出一个事件对象以后,flash player会从根容器也就是舞台开始,逐层向下寻找,直到找到最内层的目标对象(target)为止。因此从舞台目标对象一直到目标对象的所有父容器都属于捕获阶段。

2、目标阶段:当找到最内层目标对象的时候,就属于目标阶段。

3、冒泡阶段:从最内层目标对象开始,逐层向上寻找直到找到舞台根容器为止,这个过程就是冒泡阶段。

注意:两个和事件目标相关的属性(target:最内层显示对象,currentTarget:当前侦听事件的容器)

下面是我个人做的小例子:


 

下载: Bubble.as
 

01./**   02. * 作者:powerboy[动力男孩]  http://www.   03. * 日期: [08.9.2]   04. * 备注:事件流机制的3个阶段   05. */   06.package {   07.import flash.display.Sprite;   08.import flash.display.DisplayObject;   09.import flash.display.DisplayObjectContainer;   10.import flash.text.TextField;   11.import flash.text.TextFieldAutoSize;   12.import flash.events.MouseEvent;   13.public class Bubble extends Sprite {   14.    15.private var txt:TextField;   16.    17.public function Bubble() {   18.txt = new TextField();   19.txt.multiline = true;   20.txt.border = true;   21.txt.width = 500;   22.txt.height = 150;   23.txt.x = 20;   24.txt.y = 200;   25.addChild(txt);   26.    27.var roundRect:RoundRect=new RoundRect();   28.roundRect.name = "roundRect";   29.roundRect.fill(0x000000);   30.    31.var ellipse:Ellipse=new Ellipse();   32.ellipse.name = "ellipse";   33.ellipse.fill(0x00ff00);   34.ellipse.move(20,20);   35.    36.var circle:Circle=new Circle();   37.circle.name = "circle";   38.circle.fill(0x0000ff);   39.circle.move(100,80);   40.    41.addChild(roundRect);   42.roundRect.addChild(ellipse);   43.ellipse.addChild(circle);   44.    45.roundRect.addEventListener(MouseEvent.MOUSE_OVER,roundRectRollOverHandler,true);   46.ellipse.addEventListener(MouseEvent.MOUSE_OVER,ellipseRollOverHandler,true);   47.circle.addEventListener(MouseEvent.MOUSE_OVER,circleRollOverHandler,true);   48.    49.roundRect.addEventListener(MouseEvent.MOUSE_OVER,roundRectRollOverHandler);   50.ellipse.addEventListener(MouseEvent.MOUSE_OVER,ellipseRollOverHandler);   51.circle.addEventListener(MouseEvent.MOUSE_OVER,circleRollOverHandler);   52.}   53.    54.private function roundRectRollOverHandler(e:MouseEvent):void {   55.handleStage(e.target.name,e.currentTarget.name,e.eventPhase);   56.}   57.private function ellipseRollOverHandler(e:MouseEvent):void {   58.handleStage(e.target.name,e.currentTarget.name,e.eventPhase);   59.}   60.private function circleRollOverHandler(e:MouseEvent):void {   61.handleStage(e.target.name,e.currentTarget.name,e.eventPhase);   62.}   63.private function handleStage(target:String,currentTarget:String,eventPhase:Number):void{   64.var str:String;   65.switch(eventPhase){   66.case 1:   67.str = "捕获阶段 ↓";   68.break;   69.case 2:   70.str = "目标阶段 →";   71.break;   72.case 3:   73.str = "冒泡阶段 ↑";   74.break;   75.}   76.txt.appendText("最内层显示对象:"+target+"  当前侦听事件的容器:"+currentTarget+"  事件流机制:"+str+"\n");   77.txt.scrollV = txt.maxScrollV;   78.}   79.}   80.}   81.    82.import flash.display.Sprite;   83.import flash.events.MouseEvent;   84.import flash.display.DisplayObjectContainer;   85.class MyShape extends Sprite {   86.    87.public function MyShape() {   88.    89.}   90.protected function drawing():void {   91.}   92.    93.public function fill(color:uint):void {   94.this.graphics.beginFill(color);   95.drawing();   96.this.graphics.endFill();   97.}   98.public function move(x:int,y:int):void {   99.this.x = x;   100.this.y = y;   101.}   102.}   103.//圆角矩形   104.class RoundRect extends MyShape {   105.public function RoundRect() {   106.super();   107.}   108.override protected function drawing():void {   109.this.graphics.drawRoundRect(0,0,200,50,10,10);   110.}   111.}   112.//椭圆形   113.class Ellipse extends MyShape {   114.public function Ellipse() {   115.super();   116.}   117.override protected function drawing():void {   118.this.graphics.drawEllipse(0,0,200,50);   119.}   120.}   121.//圆形   122.class Circle extends MyShape {   123.public function Circle() {   124.super();   125.}   126.override protected function drawing():void {   127.this.graphics.drawCircle(0,0,50);   128.}   129.}
文章来源: 我要学flash网(www.) 原文地址:http://www./article/as3/201107/02-14478.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多