2013-02-12 81 views
0

任何人都可以看到我如何做撤銷和重做功能?所以這是我目前的動作腳本。我不知道如何做到這一點,我看到一些網站的例子,行動腳本是要長期站下。 PLS展示一個簡單的辦法,我可以使這項工作..動作腳本3繪製應用程序撤消和重做功能

抱歉語法錯誤...

import flash.display.MovieClip; 
import flash.events.MouseEvent; 

var pen_mc:MovieClip; 
var drawing:Boolean = false; 
var penSize:uint = 1; 
var penColor:Number = 0x000000; 

var shapes:Vector.<Shape>=new Vector.<Shape>(); 
var position:int=0; 
const MAX_UNDO:int=10; 


function init():void{ 

pen_mc = new MovieClip(); 
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
stage.addEventListener(MouseEvent.MOUSE_MOVE, isDrawing); 
stage.addEventListener(MouseEvent.MOUSE_UP, finishedDrawing); 
addChild(pen_mc); 

} 

init(); 

function startDrawing(e:MouseEvent):void{ 

trace("Pen Has started drawing"); 

drawing = true; 
pen_mc.graphics.lineStyle(penSize, penColor); 
pen_mc.graphics.moveTo(mouseX, mouseY); 


} 

function isDrawing(e:MouseEvent):void{ 
if(drawing){ 

    pen_mc.graphics.lineTo(mouseX, mouseY); 
} 

} 


function finishedDrawing(e:MouseEvent):void{ 

    trace("finished drawing"); 
    drawing = false; 

    var sh:Shape=new Shape(); 
    sh.graphics.copyFrom(pen_mc.graphics); // put current state into the vector 
    shapes.push(sh); 
    if (shapes.length>MAX_UNDO) shapes.unshift(); // drop oldest state 
    position=shapes.indexOf(sh); 
} 
function undo():void { 
    if (position>0) { 
     position--; 
     pen_mc.graphics.copyFrom(shapes[position].graphics); 
    } // else can't undo 
} 
function redo():void { 
    if (position+1<shapes.length) { 
     position++; 
     pen_mc.graphics.copyFrom(shapes[position].graphics); 
    } // else can't redo 
} 


function btn_undo(e:MouseEvent):void 
     { 
      undo(); 
     } 

function btn_redo(e:MouseEvent):void 
     { 
      redo(); 
     } 

undo_btn.addEventListener(MouseEvent.CLICK, btn_undo); 
redo_btn.addEventListener(MouseEvent.CLICK, btn_redo); 

回答

0

可以在Shape.graphics使用copyFrom()存儲當前的狀態,同樣以「重做「,因爲你的畫布是一個形狀。

var shapes:Vector.<Shape>=new Vector.<Shape>(); 
var position:int=0; 
const MAX_UNDO:int=10; 
... 
function finishedDrawing(e:MouseEvent):void{ 

    trace("finished drawing"); 
    drawing = false; 

    var sh:Shape=new Shape(); 
    sh.graphics.copyFrom(penMC.graphics); // put current state into the vector 
    shapes.push(sh); 
    if (shapes.length>MAX_UNDO) shapes.unshift(); // drop oldest state 
    position=shapes.indexOf(sh); 
} 
function undo():void { 
    if (position>0) { 
     position--; 
     penMC.graphics.copyFrom(shapes[position].graphics); 
    } // else can't undo 
} 
function redo():void { 
    if (position+1<shapes.length) { 
     position++; 
     penMC.graphics.copyFrom(shapes[position].graphics); 
    } // else can't redo 
} 

這種方法缺乏一些特徵,如下降的一部分撤消/重做堆棧如果第一撤消到某一點,然後拉伸。您可以嘗試自己添加此功能。

+0

它不工作仍然存在。它應該擦除當前和以前的繪圖線?我真的需要這個功能... – 2013-02-12 11:25:23

+0

每撤消一行。 – Vesper 2013-02-12 12:45:55

+0

如果您遇到麻煩,請編輯問題並在其中放入當前代碼。 – Vesper 2013-02-13 05:18:19

相關問題