package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent; //import MouseEvent 包
/**
* @Written by Leezhm, 6th Jun, 2009
* @Contact :
[email protected] * @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);
}
}
}