Draw line from object to Mouse (AS3)

心不动则不痛 提交于 2019-12-13 15:21:46

问题


My code is here:

        graphics.clear();
        graphics.lineStyle(1, 0, 1);
        graphics.moveTo(cannon.x, cannon.y);
        graphics.lineTo(mouseX, mouseY);

It doesn't seem to be drawing anything. BTW it's in ENTER_FRAME right now.


回答1:


refer a following code. this is my simple code draw app. And paste the code below:

startX and startY should instead changes to the your object(cannon).

If you want remove prior line. call a this.graphics.clear(); at onDrawReady Handler.

var isDrawingReady:Boolean;
var startX:Number, startY:Number;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDrawReady);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrawStop);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
function onDrawReady(e:MouseEvent):void
{
    startX = e.stageX;
    startY = e.stageY;
    isDrawingReady = true;
}

function onDraw(e:MouseEvent):void
{
    if(isDrawingReady)
    {
        this.graphics.lineStyle(2,0xff0000);
        this.graphics.moveTo(startX,startY);
        this.graphics.lineTo(e.stageX,e.stageY);

        startX = e.stageX;
        startY = e.stageY;
    }

    e.updateAfterEvent();
}

function onDrawStop(e:MouseEvent):void
{
    isDrawingReady = false;
}

EDIT

If you want always draw line when mouse move. tried as follows:

var startX:Number, startY:Number;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
function onDraw(e:MouseEvent):void
{
    this.graphics.lineStyle(2,0xff0000);
    this.graphics.moveTo(startX,startY);
    this.graphics.lineTo(e.stageX,e.stageY);

    startX = e.stageX;
    startY = e.stageY;

    e.updateAfterEvent();
}


来源:https://stackoverflow.com/questions/15035665/draw-line-from-object-to-mouse-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!