2

我正在尝试编写一个简单的 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;
            }
        }
    }
}
4

2 回答 2

3

首先,设置width/height一个空精灵的 不会做任何事情。如果你不想有一个尺寸为 200/100 的空精灵,你需要在其中绘制一些东西。

我修改了您的课程,以便在您将其添加到舞台后开始工作:

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);
            //clear board to white
            board.graphics.beginFill(0xffffff,1);
            board.graphics.drawRect(0,0,200,100);
            board.graphics.endFill();

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            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;
            }
        }
    }
}
于 2013-03-19T15:25:50.297 回答
0

关于Barış Uşaklı 先生的回答,我修改了这个类,所以它是完全可用的。

此外,我修改了事件处理以提高性能,因为添加新的事件侦听器和删除它们真的很慢。

初始化:

var drawingBoard:Drawing = new Drawing(stage.stageWidth, stage.stageHeight);
addChild(drawingBoard);

清算:

drawingBoard.clearDrawing();

修改颜色和铅笔宽度:

drawingBoard.drawingColor = 0x123456;
drawingBoard.drawindWidth = 16;

班级:

package {
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    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();
        private var isDrawing:Boolean = false;

        public var drawingColor:uint = 0x6666aa;
        public var drawingWidth:int = 2;

        public function Drawing(width:int, height:int) {
            trace("Drawing initialized.");

            addChild(board);

            board.graphics.beginFill(0xffffff, 0); // I like transparent things
            board.graphics.drawRect(0, 0, width, height);
            board.graphics.endFill();

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(event:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
            addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
            addEventListener(MouseEvent.MOUSE_OUT, stopDrawing);
            addEventListener(MouseEvent.MOUSE_MOVE, handleDrawing);
        }

        public function clearDrawing():void {
            board.graphics.clear();
        }

        private function startDrawing(event:MouseEvent):void {
            trace("Start drawing.");
            isDrawing = true;
        }

        private function stopDrawing(event:MouseEvent):void {
            trace("Stop drawing.");

            prevX = 0;
            prevY = 0;
            isDrawing = false;
        }

        private function handleDrawing(event:MouseEvent):void {
            if (isDrawing == true){
                trace("Handling drawing at " + event.localX + " x " + event.localY);

                if (prevX != 0 && prevY != 0) {
                    board.graphics.lineStyle(drawingWidth, drawingColor, 1);
                    board.graphics.moveTo(prevX, prevY);
                    board.graphics.lineTo(event.localX, event.localY);
                }

                prevX = event.localX;
                prevY = event.localY;
            }
        }
    }
}
于 2013-03-19T17:07:08.563 回答