首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AS3:现场绘图?

AS3:现场绘图?
EN

Stack Overflow用户
提问于 2011-10-13 00:11:21
回答 2查看 1.9K关注 0票数 1

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

下面是一个例子:

代码语言:javascript
运行
复制
        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);
    }

编辑:i使用以下示例成功地使其工作:http://www.foundation-flash.com/tutorials/as3drawingbymouse/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-13 01:07:30

您的应用程序在各种方面都有根本的缺陷。

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

代码语言:javascript
运行
复制
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();
        }

    }
}
票数 3
EN

Stack Overflow用户

发布于 2011-10-13 01:10:09

并不奇怪,单击鼠标时会分派单个MOUSE_DOWN事件,然后MOUSE_MOVE事件就会被分派出去。

你可以简单地追踪你的事件,找出到底发生了什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7748021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档