[英]How to implement Undo and Redo feature in as3
我將要創建一個應用程序,以實現撤消和重做功能。 在應用程序中,舞台上將有多個對象,用戶可以自定義對象的位置。 但是,當用戶單擊“撤消”時,對象將返回其默認位置,並且在單擊“重做”后,對象將移動到新位置。
所以我的問題是如何在我的應用程序中應用這些功能? 有圖書館或第三方類嗎?
有人能幫我嗎?
提前致謝。
看一下命令模式 。 非常適合撤消/重做類型的問題。
如果只需要兩個不同的狀態(默認狀態和“已更改”狀態),則實現起來應該很簡單。 將每個對象的新值和更新后的值存儲在數組中,並在按“撤消”或“重做”時從相關數組設置位置。
如果您需要更復雜的內容,則可以將所做的所有操作都存儲為“操作”,這是不可撤消的。 例如,有一個帶有幾個子類(例如“ Move”,“ ChangeColor”等)的類(例如“ Action”)。 它們包含執行的更改(我們應移動多少像素)以及執行操作的方法(將對象移動X像素)和撤消操作(將對象移動-X像素)。
我能想到的最簡單的方法是創建一個動作對象,在該對象中您具有舊狀態和新狀態以及已更改的對象。
每次觸發具有撤消功能的操作時,請在操作對象中填寫此數據
當您點擊撤消時,恢復到對象的前一狀態,並將操作推入重做堆棧。
這應該能夠處理最常見的更改。 諸如刪除/取消刪除之類的操作需要一些額外的調整。 我只會在刪除時放入所有屬性,並在處理撤消時使用create函數。
我建議為前后狀態使用參數和值組合的字符串,這樣就可以處理其他功能,而無需更改撤消/重做功能。
這更多是一種快速而骯臟的方式,您也可以使用設計模式和適當的OOP處理方式來做到這一點,正如其他人所提到的那樣。
package com
{
import flashx.undo.IOperation;
public class UndoOperation implements IOperation
{
private var _previousX:Number = 0;
private var _previousY:Number = 0;
private var _previousObj:Object = new Object();
private var _currentX:Number = 0;
private var _currentY:Number = 0;
private var _currentObj:Object = new Object();
private var _shape:Object = new Object();
public function TransformOperation(currentObj:Object,previousObj:Object,previousX:Number, previousY:Number, currentX:Number, currentY:Number)
{
_previousX = previousX;
_previousY = previousY;
_previousObj = previousObj;
_currentX = currentX;
_currentY = currentY;
_currentObj = currentObj;
_shape = _currentObj;
trace('Transform _previousX: '+ _previousX +' _previousY: ' +_previousY+' _currentObj '+_currentObj+' _shape '+_shape);
trace( 'trans _currentX '+ _currentX +' '+'_currentY '+ _currentY );
}
public function performUndo():void
{
_shape = _currentObj;
//trace('_shape.x '+_shape.x+" _shape.y "+_shape.y);
_shape.x = _previousX;
_shape.y = _previousY;
//trace(_previousX +' '+ _previousY +' '+ _previousWidth +' '+ _previousHeight +' '+ _previousScaleX +' '+ _previousScaleY +' '+ _previousRotation);
//trace('_shape.x '+_shape.x+" _shape.y "+_shape.y);
//trace('_currentX '+ _currentX +' '+'_currentY '+ _currentY);
}
public function performRedo():void
{
_shape.x = _currentX;
_shape.y = _currentY;
trace(_currentX+' '+ _currentY);
}
}
}
這是我的自定義類,用於在舞台上撤消和重做多個對象。 有不同的模式可用於撤消和重做操作,但是對我來說,這是撤消和重做的簡單方法。 我已經在我的項目中完美且成功地實現了這一點。
要使用此類僅導入該類
import flashx.undo.UndoManager;
在那之后
public static var _undo:UndoManager=new UndoManager();
public static var _redo:UndoManager=new UndoManager();
public static var PrevX:Number=0;
public static var PrevY:Number=0;
var operation:TransformOperation = new TransformOperation(this,this,PrevX,PrevY,this.x,this.y);
_undo.pushUndo(operation);
之后,創建撤消和重做的點擊事件:
public static function Undo(e:MouseEvent):void
{
_redo.pushRedo(_undo.peekUndo());
_undo.undo();
if(_undo.peekUndo()==null)
{
PlayerPosition.getClass.getChildByName("toolbar_mc").getChildByName("undo_mc").removeEventListener(MouseEvent.CLICK,Undo);
}
PlayerPosition.getClass.getChildByName("toolbar_mc").getChildByName("redo_mc").addEventListener(MouseEvent.CLICK,Redo);
}
public static function Redo(e:MouseEvent):void
{
_undo.pushUndo(_redo.peekRedo());
_redo.redo();
if(_redo.peekRedo()==null)
{ PlayerPosition.getClass.getChildByName("toolbar_mc").getChildByName("redo_mc").removeEventListener(MouseEvent.CLICK,Redo);
}
PlayerPosition.getClass.getChildByName("toolbar_mc").getChildByName("undo_mc").addEventListener(MouseEvent.CLICK,Undo);
}
對我來說,這很好...希望也能對其他人有所幫助... :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.