2013-02-12 84 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

相关问题