我正在尝试编写一个简单的 as3 绘图类,但它不起作用。甚至事件都没有触发。可能是什么问题呢?
用法:
var drawingBoard:Drawing = new Drawing();
drawingBoard.x = 0;
drawingBoard.y = 0;
drawingBoard.width = 200;
drawingBoard.height = 100;
addChild(drawingBoard);
班级:
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Drawing extends Sprite {
private var prevX:Number = 0;
private var prevY:Number = 0;
private var board:Shape = new Shape();
public function Drawing() {
trace("Drawing initialized.");
addChild(board);
addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
addEventListener(MouseEvent.MOUSE_OUT, stopDrawing);
}
private function startDrawing(event:MouseEvent):void {
trace("Start drawing.");
addEventListener(MouseEvent.MOUSE_MOVE,handleDrawing);
}
private function stopDrawing(event:MouseEvent):void {
trace("Stop drawing.");
prevX = 0;
prevY = 0;
removeEventListener(MouseEvent.MOUSE_MOVE,handleDrawing,false);
}
private function handleDrawing(event:MouseEvent):void {
trace("Handling drawing.");
if (prevX == 0 || prevY == 0) {
prevX = event.localX;
prevY = event.localY;
} else {
board.graphics.lineStyle(10, 0x000000, 1);
board.graphics.moveTo(prevX, prevY);
board.graphics.lineTo(event.localX, event.localY);
prevX = event.localX;
prevY = event.localY;
}
}
}
}