绘制应用程序撤消和重做功能动作脚本3

时间:2013-02-11 17:30:04

标签: actionscript-3 function undo redo

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

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

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

这个,o使用两个图形,不是那种方式

我接近这个的方法是创建一个lineInfo类(看看你如何只使用行),并获得startpoint,endpoint,lineColour,lineWidth和lineAlpha的信息。

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

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