public function drag(e:MouseEvent)
{
lineDraw(mouseX, mouseY);
e.updateAfterEvent();
}
public function lineDraw(X:int, Y:int):void
{
currentX = X;
currentY = Y;
graphics.lineStyle(size, color)
graphics.moveTo(previousX, previousY)
graphics.lineTo(currentX, currentY)
previousX = currentX;
previousY = currentY;
}
我制作的非常简单的代码允许我用鼠标画线。在MOUSE_MOVE
之后MOUSE_DOWN
触发功能拖动。
我的问题是:如何清除最后一行?基本上,ctrl+z/undo
函数可以重复我想要的次数。
我是否必须完全重写我的代码并将每一行绘制成一个数组,然后在数组中向后工作,在我点击时删除行"撤消"?或者还有另一种更好,更简单的解决方案吗?
谢谢! :)
答案 0 :(得分:1)
以下是一个示例:请参阅代码注释以获取解释
private var undoStates:Vector.<Shape> = new Vector.<Shape>(); //holds all your undo states (HAS TO BE SHAPE SINCE AS3 DOESN"T LET YOU ISNTANTIATE A GRAPHICS OBJECT DIRECTLY)
private var redoStates:Vector.<Shape> = new Vector.<Shape>(); //holds all your redo states
private var undoLevels:int = 1; //how many undo levels you'd like to have
private var redoLevels:int = 1;
public function undo() {
if (undoStates.length > 0) { //make sure there is an undo state before preceeding
//add redo state
redoStates.push(getState());
//if redo states are more than redoLevels, remove the oldest one
if (redoStates.length > redoLevels) redoStates.splice(0, 1);
//make the the last undo state the current graphics
graphics.clear(); //not sure if this line is required, can't recall if copyFrom clears first
graphics.copyFrom(undoStates.pop().graphics); //pop removes the last item from the array and returns it
}
}
public function redo() {
if (redoStates.length > 0) {
//add undo state
addUndo();
//make the the last undo state the current graphics
graphics.clear(); //not sure if copy from (the next line) clears or not
graphics.copyFrom(redoStates.pop().graphics);
}
}
private function getState():Shape{
var state:Shape = new Shape();
state.graphics.copyFrom(graphics); //copy the current graphics into a new Graphics object
return state;
}
private function addUndo():void {
undoStates.push(getState()); //add the current state to the undo array
//if more undo states than undoLevels, remove the oldest one
if (undoStates.length > undoLevels) undoStates.splice(0, 1);
}
public function lineDraw(X:int, Y:int):void {
currentX = X;
currentY = Y;
//create undo state before we draw more
addUndo();
graphics.lineStyle(size, color)
graphics.moveTo(previousX, previousY)
graphics.lineTo(currentX, currentY)
previousX = currentX;
previousY = currentY;
}