0

谁能告诉我如何进行撤消和重做功能?所以这是我当前的动作脚本。我不知道该怎么做,我在一些网站上看到了一些例子,动作脚本很长,无法理解。请展示一个简单的方法,我可以使这项工作..

对不起语法不好...

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

1 回答 1

0

您可以copyFrom()在 Shape.graphics 中使用来存储当前状态,并且与“重做”相同,因为您的画布是一个形状。

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
}

这种方法缺乏一些特性,例如如果先撤消到某个点,然后再绘制,则丢弃部分撤消/重做堆栈。您可以尝试自己添加此功能。

于 2013-02-12T09:41:04.403 回答