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

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

问题描述

我要创建的应用程序,我必须实施的撤消和重做功能。 在应用将有位于舞台和用户的多个对象可以自主 的对象的位置。但是,当用户点击撤销的对象回到其默认 位置和点击重做对象将移动的新位置之后。

所以我的问题是我怎么可以将这些功能在我的应用程序? 是否有任何图书馆或任何第三方类?

可以有一个人帮助我吗?

在此先感谢。

解决方案

 包COM
 {
    进口flashx.undo.IOperation;

    公共类UndoOperation实现的IOperation
    {

    私人变种_ previousX:数= 0;
    私人变种_ previousY:数= 0;
    私人变种_ previousObj:对象=新的对象();
    私人VAR _currentX:数= 0;
    私人VAR _currentY:数= 0;
    私人VAR _currentObj:对象=新的对象();
    私人VAR _shape:对象=新的对象();


    公共职能TransformOperation(currentObj:对象,previousObj:对象,previousX:数字,previousY:编号,currentX:号码,currentY:号)
    {
        _ previousX = previousX;
        _ previousY = previousY;
        _ previousObj = previousObj;

        _currentX = currentX;
        _currentY = currentY;
        _currentObj = currentObj;
        _shape = _currentObj;

        跟踪('变换_ previousX:'+ _ previousX +'_ previousY:'+ _ previousY +'_currentObj'+ _currentObj +_shape'+ _shape);
        跟踪(反_currentX'+ _currentX +''+'_ currentY'+ _currentY);

    }

    公共职能performUndo():无效
    {
        _shape = _currentObj;
        //trace('_shape.x'+ _shape.x +_shape.y+ _shape.y);
        _shape.x = _ previousX;
        _shape.y = _ previousY;
        //跟踪(_ previousX +''+ _ previousY +''+ _ previousWidth +''+ _ previousHeight +''+ _ previousScaleX +''+ _ previousScaleY +''+ _ previousRotation);
        //trace('_shape.x'+ _shape.x +_shape.y+ _shape.y);
        //跟踪('_ currentX'+ _currentX +''+'_ currentY'+ _currentY);
    }

    公共职能performRedo():无效
    {
        _shape.x = _currentX;
        _shape.y = _currentY;
        跟踪(_currentX +''+ _currentY);
    }
  }
}
 

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

要使用这个类只导入类

 进口flashx.undo.UndoManager;
 

和后

 公共静态无功_undo:UndoManager的=新的UndoManager();
            公共静态无功_redo:UndoManager的=新的UndoManager();
            公共静态无功preVX:数= 0;
            公共静态无功preVY:数= 0;
            VAR操作:TransformOperation =新TransformOperation(这个,这​​个,preVX,preVY,this.x,this.y);
           _undo.pushUndo(操作);
 

之后,创建单击事件,撤消和重做:

 公共静态函数撤消(E:MouseEvent)方法:无效
    {
        _redo.pushRedo(_undo.peekUndo());
        _undo.undo();
        如果(_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);
    }

    公共静态函数重做(E:MouseEvent)方法:无效
    {
        _undo.pushUndo(_redo.peekRedo());
        _redo.redo();
        如果(_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);
    }
 

对于我这个工作得很好...希望这可以帮助别人太...:)

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?

Can some one help me?

Thanks in advance.

解决方案

 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.

To Use this class only import the class

                 import flashx.undo.UndoManager;

and after that

            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天全站免登陆