2014-10-26 65 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