0

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

对不起语法不好...

var drawingLine:Shape=new Shape();
board.addChild(drawingLine);

var doDraw:Boolean=false;

var lineSize:Number=7;

var activeColor:uint = 0x000000;

function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}


function MouseDown(e:MouseEvent):void{

doDraw=true;

drawingLine.graphics.moveTo(drawingLine.mouseX, drawingLine.mouseY);
drawingLine.graphics.lineStyle(lineSize, activeColor);
board.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);

}

function MouseMove(e:MouseEvent):void{
        var curX:Number=drawingLine.mouseX;
        var curY:Number=drawingLine.mouseY;

        if(doDraw && checkCoords(curX,curY)){

            if(active=="Line"){

                    clearTemp();

            temporaryDrawing.graphics.lineTo(curX,curY);

            }else{

            drawingLine.graphics.lineTo(curX,curY);

            }


            e.updateAfterEvent();
            }
}

function MouseUp(event:MouseEvent):void{

doDraw=false;

}


btn_Pencil.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
4

2 回答 2

0

单击数组或向量后,您可以保存所有动作,然后如果要撤消,则重绘数组中除最后一个以外的所有动作。

可能是这样的:

var drawingLine:Shape=new Shape();
board.addChild(drawingLine);

// MOVEMENTS INFORMATION
var movements:Array = new Array();
var doDraw:Boolean = false;
var cacheIndex:int = 0;

var lineSize:Number=7;

var activeColor:uint = 0x000000;

function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}


function MouseDown(e:MouseEvent):void{

function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}


function MouseDown(e:MouseEvent):void{

    doDraw=true;

    drawingLine.graphics.moveTo(drawingLine.mouseX, drawingLine.mouseY);
    drawingLine.graphics.lineStyle(lineSize, activeColor);
    board.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
    lastTracing = {
        mainPoint: {
            x:drawingLine.mouseX, 
            y:drawingLine.mouseY
        }, 
        lineSize: lineSize, 
        activeColor:activeColor, 
        points:new Array()
    }
    cacheIndex = 0;
    movements.splice(movements.length - cacheIndex, cacheIndex);
    movements.push(lastTracing );

}

function MouseMove(e:MouseEvent):void{
    var curX:Number=drawingLine.mouseX;
    var curY:Number=drawingLine.mouseY;

    if(doDraw && checkCoords(curX,curY)){
        if(active=="Line"){

            clearTemp();

            temporaryDrawing.graphics.lineTo(curX,curY);

        }else{

            drawingLine.graphics.lineTo(curX,curY);
            lastTracing.points.push({x:curX, y:curY});

        }


            e.updateAfterEvent();
    }
}

function MouseUp(event:MouseEvent):void{

doDraw=false;

}
function undoFunction(event:MouseEvent=null):void {
    if(cacheIndex+1 <= movements.length){
        cacheIndex++;
    }
    drawCache();
}
function redoFunction(event:MouseEvent = null):void {
    if(cacheIndex-1 >= 0){
        cacheIndex--;
    }
    drawCache();
}
function drawCache():void {
    for(var i:uint=0;i<(movements.length-cacheIndex);i++){
         var tracingInfo:Object = movements[i];

         drawingLine.graphics.clear();
         drawingLine.graphics.moveTo(tracingInfo.mainPoint.x, tracingInfo.mainPoint.y);
         drawingLine.graphics.lineStyle(tracingInfo.lineSize, tracingInfo.activeColor);

         for(var j:uint=0;j<tracingInfo.points.length;j++){
              drawingLine.graphics.lineTo(tracingInfo.points[j].x,tracingInfo.points[j].y);
         }
    }
}

btn_Pencil.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
于 2013-02-11T21:56:36.930 回答
0

这,o 使用两个图形,不会是要走的路

我解决这个问题的方法是创建一个 lineInfo 类(仅查看您如何使用线条),并拥有起点、终点、lineColour、lineWidth 和 lineAlpha 的信息。

然后创建一个数组或 Vector 并使用新的 LineInfo 类填充它,并使用线条更新图形。如果需要撤消,可以将最后一项的use值设置为false,从数组的指令中重新绘制图形。创建一个撤消步骤整数,用于跟踪应省略多少(从后面数)操作。

为了提高性能,您最多可以创建 25 条指令,并创建一个图形来缓存未在撤消列表中的项目。

于 2013-02-11T21:57:07.760 回答