从对象到鼠标画线(AS3)

时间:2013-02-23 00:47:37

标签: actionscript-3 flash

我的代码在这里:

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

它似乎没有绘制任何东西。 顺便说一下,它现在在ENTER_FRAME中。

1 个答案:

答案 0 :(得分:4)

参考以下代码。这是我的简单代码绘制应用程序。并粘贴以下代码:

startXstartY应改为对象(cannon)。

如果你想删除前一行。在this.graphics.clear();处理程序处拨打onDrawReady

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;
}

修改

如果你想在鼠标移动时总是画线。尝试如下:

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();
}