簡體   English   中英

動作腳本3繪圖應用程序的撤消和重做功能

[英]Action script 3 drawing app undo and redo function

誰能告訴我如何進行撤消和重做功能? 這是我當前的動作腳本。 我不知道該怎么做,我在一些網站上看到了一些例子,動作腳本已久了。 請顯示一個簡單的方法,我可以使這項工作..

對不起,語法不好。

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

您可以在Shape.graphics中使用copyFrom()來存儲當前條件,並且與“重做”相同,因為您的畫布是一個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
}

這種方法缺少一些功能,例如,如果先撤消撤消/重做堆棧的一部分到特定點,然后繪制,則會丟失一部分。 您可以嘗試自己添加此功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM