關于Actionscript 3.0的事件機制,以后分模塊一個一個介紹,最后形成一個完整的文檔??戳说谝徽拢驼f說鼠標事件,順便說下我遇到的一個問題。Actionscript 3.0鼠標事件無非就是監聽和捕獲鼠標事件,比如Click,DoubleClick等,AS3中鼠標事件包是在flash.events.MouseEvent中。
在這里貼一個簡單ActionScript 3.0實現寫字板程序代碼,結合代碼來理解鼠標的監聽,捕獲處理過程。
復制代碼 代碼如下:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent; //import MouseEvent 包
/**
* @Written by Leezhm, 6th Jun, 2009
* @Contact : Leezhm@126.com
* @author : Leezhm
*
**Last Modified by Leezhm on 6th Jun, 2009
*
*/
[SWF(height = "450", width = "600", backgroundColor = "0xFFFFFF", frameRate = "31")] //設置應用程序屬性
public class Main extends Sprite
{
public function Main():void
{
if (stage)
{
Init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, Init);
}
}
private function Init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, Init);
// entry point
addEventListener(Event.ENTER_FRAME, OnEnterFrameHandler); //監聽ENTER_FRAME事件,一個重要的事件
}
private function OnEnterFrameHandler(e:Event):void //ENTER_FRAME事件的處理函數
{
this.DrawGraphic();
}
private function DrawGraphic():void
{
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDownHandler); //監聽鼠標Down事件
}
private function OnMouseDownHandler(e:MouseEvent):void //處理鼠標Down事件
{
this.graphics.lineStyle(2, 0, 1);
this.graphics.moveTo(this.mouseX, this.mouseY);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseMoveHandler); //監聽鼠標MOVE事件
}
private function OnMouseMoveHandler(e:MouseEvent):void //鼠標MOVE事件處理函數
{
this.graphics.lineTo(this.mouseX, this.mouseY);
this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUpHandler); //監聽鼠標UP事件
}
private function OnMouseUpHandler(e:MouseEvent):void //處理鼠標UP事件
{ // 移除對鼠標DOWN、MOVE和UP事件的監聽
this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, OnMouseDownHandler);
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseMoveHandler);
this.stage.removeEventListener(MouseEvent.MOUSE_UP, OnMouseUpHandler);
}
}
}
其實看上面的代碼就很簡單地理解Actionscript 3.0中鼠標事件的處理過程,順便傳一張效果圖片。
好了說一個我曾經犯的錯誤,代碼如下:
復制代碼 代碼如下:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class A extends Sprite
{
public function A()
{
graphics.beginFill(0xFF0000);
graphics.drawCircle(100,100,40);
graphics.endFill();
addEventListener(MouseEvent.CLICK, testclick);
}
private function testclick(event:MouseEvent):void {
trace("Hello World!!!");
}
}
}
實際上上面的這段代碼并不響應鼠標的CLICK事件,為什么,開始我也花了很長時間來查找原因。其實這涉及到Actionscript 3.0的事件實現機制,在這里簡單解釋下:
原因很簡單,因為上面那樣的一個文檔類是空的,根本就沒有任何顯示對象,所以就不會響應鼠標事件了(那個DrawCircle 不能算顯示對象,它只能算是一個背景而已,并不在Actionscript的顯示列表中 ,所以Actionscript事件機制中就不可能向它分發事件消息)。但下面對以上代碼做一下修改就可以。
復制代碼 代碼如下:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class A extends Sprite {
public function A() {
var _sp:Sprite=new Sprite()
_sp.graphics.beginFill(0xFF0000);
_sp.graphics.drawCircle(100,100,40);
_sp.graphics.endFill();
addChild(sp)
_sp.addEventListener(MouseEvent.CLICK, testclick);
}
private function testclick(event:MouseEvent):void {
trace("Hello World!!!");
}
}
}
對比倆代碼就可以發現,后面的加了一個顯示對象,并在顯示對象上監聽鼠標事件。 當然也可以想第一段代碼中那樣在Stage上監聽。
注意,直接this.addEventListener這樣來監聽的是root,并不是Stage這樣的DisplayObject對象,同樣是不可以的。