在上一篇中,事件的响应是由addEventListener来实现的,鼠标事件的响应也不例外,但是不同的是事件不一样了,在使用完事件后,要记得移除相应的事件
在这一类中用到了对象的创建,addChild,MoveTo,LineTo,Fill,EndFill()等方法
Code
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class MouseEvent extends Sprite
{
private var _sprint:Sprite;
public function MouseEvent()
{
super();
_sprint=new Sprite();
addChild(_sprint);
_sprint.graphics.beginFill(0xffffff);
_sprint.graphics.drawRect(0,0,500,400);
_sprint.graphics.endFill();
_sprint.addEventListener(flash.events.MouseEvent.MOUSE_DOWN,OnMouseDown);
_sprint.addEventListener(flash.events.MouseEvent.MOUSE_UP,OnMouseUp);
}
public function OnMouseDown(event:flash.events.MouseEvent):void
{
_sprint.graphics.lineStyle(1,2,1);
_sprint.graphics.moveTo(mouseX,mouseY);
_sprint.addEventListener(flash.events.MouseEvent.MOUSE_MOVE,OnMouseMove);
}
public function OnMouseUp(event:flash.events.MouseEvent):void
{
_sprint.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE,OnMouseMove);
}
public function OnMouseMove(event:flash.events.MouseEvent):void
{
_sprint.graphics.lineTo(mouseX,mouseY);
}
}
}