AS3:现场绘画?

时间:2011-10-13 00:11:22

标签: actionscript-3 drawing

我有一个奇怪的问题,如果我在移动鼠标时使用 graphics.lineTo MOUSE_MOVE ),则会实时创建这些行。但是,如果我将其更改为 MOUSE_DOWN ,则线条笔直,如果仍然按下鼠标则无响应。

以下是一个例子:

        stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, draw);


    private function follow(e:MouseEvent){

        trace(e.localY);

        activeChalk.x = e.stageX;
        activeChalk.y = e.stageY;

    }


    private function draw(e:MouseEvent){

        trace(e.localY);

        activeChalk.x = e.stageX;
        activeChalk.y = e.stageY;
        board.graphics.lineTo(e.stageX,e.stageY);
    }

编辑:我设法使用以下示例使其工作: http://www.foundation-flash.com/tutorials/as3drawingbymouse/

2 个答案:

答案 0 :(得分:3)

您的申请在各方面存在根本缺陷。

这里有一个非常简单的绘图实现,而不是详细介绍:

package
{
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 30)]
    public class Chalk extends Sprite
    {

        protected var lastPoint:Point;

        public function Chalk()
        {
            super();

            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            initializeDrawing();
        }

        protected function initializeDrawing():void
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        protected function mouseDownHandler(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

            // mark mouse down location
            lastPoint = new Point(mouseX, mouseY);

            // listen for movement or up/out
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            stage.addEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler);
        }

        protected function mouseMoveHandler(event:MouseEvent):void
        {
            var g:Graphics = graphics;
            g.lineStyle(1, 0x0000ff);

            // draw line segment
            g.moveTo(lastPoint.x, lastPoint.y);
            g.lineTo(mouseX, mouseY);

            // mark end of line segment
            lastPoint = new Point(mouseX, mouseY);
        }

        protected function mouseUpHandler(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            stage.removeEventListener(MouseEvent.MOUSE_OUT, mouseUpHandler);

            // prepare for next line
            initializeDrawing();
        }

    }
}

答案 1 :(得分:0)

并不奇怪,单击鼠标时会调度单个MOUSE_DOWN事件,然后调度MOUSE_MOVE事件。

您可以简单地追踪您的活动,以确切了解正在发生的事情。