动作脚本3绘制app撤消和重做功能

时间:2013-02-12 08:44:53

标签: actionscript-3 flash drawing undo redo

任何人都可以告诉我如何进行撤消和重做功能?所以这是我当前的动作脚本。我不知道如何做到这一点,我在一些网站上看到一些例子,动作脚本是长期以来的立场。请展示一个简单的方法,我可以使这项工作..

抱歉语法不好......

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);

1 个答案:

答案 0 :(得分:0)

您可以在Shape.graphics中使用copyFrom()来存储当前条件,并使用相同的“redo”,因为您的画布是一个Shape。

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
}

这种方法缺少一些功能,如果首先撤消到某个点,然后绘制,则删除部分撤消/重做堆栈。您可以尝试自己添加此功能。