關於Actionscript 3.0中的滑鼠事件

來源:互聯網
上載者:User

                                                   關於Actionscript 3.0中的滑鼠事件

                                                                                                          --------Actionscript 3.0 CookBook (I)

 

  這本書是一本經典的書籍,說實話一些小的AS3的項目是做了好幾個,基本的文法也都知道(其實有物件導向程式設計語言的基礎後再入門一門新的語言還是很快的)。現在找到了這邊經典書籍完整版,就好好看看,順便寫一些總結。

 

  關於Actionscript 3.0的事件機制,以後分模組一個一個介紹,最後形成一個完整的文檔。看了第一章,就說說滑鼠事件,順便說下我遇到的一個問題。Actionscript 3.0滑鼠事件無非就是監聽和捕獲滑鼠事件,比如Click,DoubleClick等,AS3中滑鼠事件包是在flash.events.MouseEvent中。

 

  在這裡貼一個簡單ActionScript 3.0實現寫字板程式碼,結合代碼來理解滑鼠的監聽,捕獲處理過程。

  1. package 
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.events.MouseEvent;  //import MouseEvent 包
  6.     
  7.     /**
  8.      * @Written by Leezhm, 6th Jun, 2009
  9.      * @Contact : Leezhm@126.com
  10.      * @author  : Leezhm
  11.      * 
  12.      **Last Modified by Leezhm on 6th Jun, 2009
  13.      * 
  14.      */
  15.     [SWF(height = "450", width = "600", backgroundColor = "0xFFFFFF", frameRate = "31")] //設定應用程式屬性
  16.      
  17.     public class Main extends Sprite 
  18.     {
  19.         public function Main():void 
  20.         {
  21.             if (stage) 
  22.             {
  23.                 Init();
  24.             }
  25.             else
  26.             {
  27.                 addEventListener(Event.ADDED_TO_STAGE, Init);
  28.             }
  29.         }
  30.         
  31.         private function Init(e:Event = null):void 
  32.         {
  33.             removeEventListener(Event.ADDED_TO_STAGE, Init);
  34.             // entry point
  35.             
  36.             addEventListener(Event.ENTER_FRAME, OnEnterFrameHandler); //監聽ENTER_FRAME事件,一個重要的事件
  37.         }
  38.         
  39.         private function OnEnterFrameHandler(e:Event):void   //ENTER_FRAME事件的處理函數
  40.         {
  41.             this.DrawGraphic();
  42.         }
  43.         
  44.         private function DrawGraphic():void
  45.         {
  46.             this.stage.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDownHandler); //監聽滑鼠Down事件
  47.         }
  48.         
  49.         private function OnMouseDownHandler(e:MouseEvent):void //處理滑鼠Down事件
  50.         {
  51.             this.graphics.lineStyle(2, 0, 1);
  52.             this.graphics.moveTo(this.mouseX, this.mouseY);
  53.             this.stage.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseMoveHandler); //監聽滑鼠MOVE事件
  54.         }
  55.         
  56.         private function OnMouseMoveHandler(e:MouseEvent):void       //滑鼠MOVE事件處理函數
  57.         {
  58.             this.graphics.lineTo(this.mouseX, this.mouseY);
  59.             this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUpHandler); //監聽滑鼠UP事件
  60.         }
  61.         
  62.         private function OnMouseUpHandler(e:MouseEvent):void  //處理滑鼠UP事件
  63.         {  // 移除對滑鼠DOWN、MOVE和UP事件的監聽
  64.             this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, OnMouseDownHandler);
  65.             this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseMoveHandler);
  66.             this.stage.removeEventListener(MouseEvent.MOUSE_UP,   OnMouseUpHandler);
  67.         }
  68.     }
  69. }

  其實看上面的代碼就很簡單地理解Actionscript 3.0中滑鼠事件的處理過程,順便傳一張片。

                                        

  好了說一個我曾經犯的錯誤,代碼如下:

  1. package 
  2. {
  3.         import flash.display.Sprite;
  4.         import flash.events.MouseEvent;
  5.         
  6.         public class A extends Sprite 
  7.         {
  8.                public function A() 
  9.                {
  10.                         graphics.beginFill(0xFF0000);
  11.                         graphics.drawCircle(100,100,40);
  12.                         graphics.endFill();
  13.                         addEventListener(MouseEvent.CLICK, testclick);
  14.                 }
  15.                 private function testclick(event:MouseEvent):void {
  16.                         trace("Hello World!!!");
  17.                 }
  18.         }
  19. }

  實際上上面的這段代碼並不響應滑鼠的CLICK事件,為什麼,開始我也花了很長時間來尋找原因。其實這涉及到Actionscript 3.0的事件實現機制,在這裡簡單解釋下:

  原因很簡單,因為上面那樣的一個文檔類是空的,根本就沒有任何顯示對象,所以就不會響應滑鼠事件了(那個DrawCircle 不能算顯示對象,它只能算是一個背景而已,並不在Actionscript的顯示列表中 ,所以Actionscript事件機制中就不可能向它分發事件訊息)。但下面對以上代碼做一下修改就可以。

  1. package {
  2.         import flash.display.Sprite;
  3.         import flash.events.MouseEvent;
  4.         public class A extends Sprite {
  5.                 public function A() {
  6.                         var _sp:Sprite=new Sprite()
  7.                         _sp.graphics.beginFill(0xFF0000);
  8.                         _sp.graphics.drawCircle(100,100,40);
  9.                         _sp.graphics.endFill();
  10.                         addChild(sp)
  11.                         _sp.addEventListener(MouseEvent.CLICK, testclick);
  12.                 }
  13.                 private function testclick(event:MouseEvent):void {
  14.                         trace("Hello World!!!");
  15.                 }
  16.         }
  17. }

 

 

   對比倆代碼就可以發現,後面的加了一個顯示對象,並在顯示對象上監聽滑鼠事件。 當然也可以想第一段代碼中那樣在Stage上監聽。

 

   注意,直接this.addEventListener這樣來監聽的是root,並不是Stage這樣的DisplayObject對象,同樣是不可以的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.