下面的完整例子通过一个绘图程序演示了各种鼠标事件,无论何时鼠标按下,绘图将立即开始,当移动鼠标,一条线跟着鼠标显示出来,当松开鼠标,绘图终止:
例子代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package { import flash.display.Sprite; import flash.events.MouseEvent; public class DrawingDemo extends Sprite { // Flag to indicate whether the mouse is in draw mode private var _drawing:Boolean; public function DrawingDemo( ) {// 设置线条样式 graphics.lineStyle( 2, 0xFF0000 ); // 按住鼠标就绘图 _drawing = false; // 监听鼠标事件 stage.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); stage.addEventListener( MouseEvent.MOUSE_MOVE, draw ); stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); } public function startDrawing( event:MouseEvent ):void { // 绘制起点 graphics.moveTo( mouseX, mouseY ); _drawing = true; } public function draw( event:MouseEvent ):void { if (_drawing) { // 鼠标走到哪画到哪 graphics.lineTo( mouseX, mouseY ); } } public function stopDrawing( event:MouseEvent ):void { _drawing = false; } } } |
摘自:ActionScript 3.0 Cookbook