2

我的代码在这里:

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

好像什么都没画。顺便说一句,它现在在 ENTER_FRAME 中。

4

1 回答 1

4

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

startX而是startY应该更改为您的对象(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();
}
于 2013-02-23T01:11:47.330 回答