2014-10-26 122 views
0
public function drag(e:MouseEvent) 
    { 
     lineDraw(mouseX, mouseY); 
     e.updateAfterEvent(); 

    } 
    public function lineDraw(X:int, Y:int):void 
    { 
     currentX = X; 
     currentY = Y; 

     graphics.lineStyle(size, color) 
     graphics.moveTo(previousX, previousY) 
     graphics.lineTo(currentX, currentY) 

     previousX = currentX; 
     previousY = currentY; 
    } 

非常简单的代码,我让我用我的鼠标画线。在MOUSE_DOWN之后MOUSE_MOVE上触发功能拖动。清除最后绘制的图形? (撤消/重绘图)

我的问题是:我将如何去清除绘制的最后一行?基本上,一个ctrl+z/undo函数可以重复多次,我想要的。

我是否必须完全重写我的代码,并将每一行绘制到一个数组中,然后通过数组反向工作,删除行,因为我单击“撤消”?还是有另一个更好,更简单的解决方案呢?

谢谢! :)

+1

是的,独立的小精灵(或任何的DisplayObject使用)的将是最好的一段路要走的数组代码的注释。 'graphics'属性具有'clear()'功能,但是这会擦除整个事物,而不仅仅是最后一行。另一种方法是只存储数组中每一行的坐标,然后用白色(或任何你需要的)和相同的坐标绘制该行,但这是一种相当草率的方法。 – DodgerThud 2014-10-26 19:21:56

+0

@DodgerThud好的,我会试试看。谢谢;) – Benjamin 2014-10-26 19:34:14

+0

'Graphics'类的'copyFrom'方法在这里可以非常方便地用于“克隆” – 2014-10-26 20:02:40

回答

1

下面是一个例子:请参阅解释

private var undoStates:Vector.<Shape> = new Vector.<Shape>(); //holds all your undo states (HAS TO BE SHAPE SINCE AS3 DOESN"T LET YOU ISNTANTIATE A GRAPHICS OBJECT DIRECTLY) 
private var redoStates:Vector.<Shape> = new Vector.<Shape>(); //holds all your redo states 
private var undoLevels:int = 1; //how many undo levels you'd like to have 
private var redoLevels:int = 1; 

public function undo() { 
    if (undoStates.length > 0) { //make sure there is an undo state before preceeding 
     //add redo state 
     redoStates.push(getState()); 

     //if redo states are more than redoLevels, remove the oldest one 
     if (redoStates.length > redoLevels) redoStates.splice(0, 1); 

     //make the the last undo state the current graphics 
     graphics.clear(); //not sure if this line is required, can't recall if copyFrom clears first 
     graphics.copyFrom(undoStates.pop().graphics); //pop removes the last item from the array and returns it 
    } 
} 

public function redo() { 
    if (redoStates.length > 0) { 
     //add undo state 
     addUndo(); 

     //make the the last undo state the current graphics 
     graphics.clear(); //not sure if copy from (the next line) clears or not 
     graphics.copyFrom(redoStates.pop().graphics); 
    } 
} 

private function getState():Shape{ 
    var state:Shape = new Shape(); 
    state.graphics.copyFrom(graphics); //copy the current graphics into a new Graphics object 
    return state; 
} 

private function addUndo():void { 
    undoStates.push(getState()); //add the current state to the undo array 

    //if more undo states than undoLevels, remove the oldest one 
    if (undoStates.length > undoLevels) undoStates.splice(0, 1); 
} 

public function lineDraw(X:int, Y:int):void { 
    currentX = X; 
    currentY = Y; 


    //create undo state before we draw more 
    addUndo(); 

    graphics.lineStyle(size, color) 
    graphics.moveTo(previousX, previousY) 
    graphics.lineTo(currentX, currentY) 


    previousX = currentX; 
    previousY = currentY; 
} 
+0

我一定会考虑这一点。看起来非常有趣。目前,我通过将所有Shapes推入数组来解决undo函数,然后使用for循环清除了undoArray [s] .graphics.clear();然后拼接它。但是我仍然没有找到重新创建形状的方法,但是我会查看您在此处使用的copyFrom函数,我认为这可能有助于重做。:)谢谢! – Benjamin 2014-10-28 12:15:24