如何在 as3 中实现撤消和重做功能 [英] How to implement Undo and Redo feature in as3

查看:35
本文介绍了如何在 as3 中实现撤消和重做功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个应用程序,我必须实现一个撤消和重做功能.在应用程序中,舞台上将有多个对象,用户可以自定义物体的位置.但是当用户点击 Undo 时,对象会恢复到默认状态位置,点击重做对象后会移动到新位置.

I am going to create an application in that i have to implement an Undo and Redo feature. In the application there will be multiple objects located on stage and user can customize the position of the objects. But when user clicks on Undo the object go back to their default position and after clicking on redo object will move on the new position.

所以我的问题是如何在我的应用程序中应用这些功能?是否有任何图书馆或任何第三方课程?

So my question is how can i apply these feature in my application? Is there any library or any third party classes?

有人可以帮我吗?

提前致谢.

推荐答案

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

这是我的自定义类,用于在舞台上撤消和重做多个对象.有不同的模式可用于撤消和重做操作,但对我来说,这是撤消和重做的简单方法.我已经在我的项目中完美并成功地实现了这一点.

This is my customize class afor undo and redo multiple objects on stage. There is different pattern which can be used for the undo and redo operation but for me this is the simple way for undo and redo. I have perfectly and successfully implement this in my project.

要使用这个类只导入类

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

之后创建用于撤消和重做的点击事件:

After that create click event for undo and redo:

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

对我来说这很好用...希望这也能帮助其他人...:)

For me this works fine... Hope this helps others too...:)

这篇关于如何在 as3 中实现撤消和重做功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆