Skip to main content
markerline
Inspiring
August 23, 2013
Answered

I'm only getting one level of undo with pop method / undo button

  • August 23, 2013
  • 1 reply
  • 2304 views

Hi.  Why does this code only produce one level of undo?  I thought each time the pop method is triggered on an array the array shrinks and you can continue to pop as long as the array is not less than 0.

Here's my code:

import flash.display.MovieClip;

import flash.display.Shape;

import flash.events.MouseEvent;

import flash.events.Event;

//http://www.kirupa.com/forum/showthread.php?309502-As3-Spiral-effect .. this is the one used previously in other files

//http://hub.tutsplus.com/tutorials/create-a-basic-drawing-application-in-flash--active-1627 .. this is the one used here

var my_mc:MovieClip=new MC();

var container:MovieClip=new MovieClip();

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);

stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

var shape:Shape=new Shape();

var myArr:Array=new Array();

var myArrRedo:Array = new Array();

var myArrObj:Array= new Array();

var undo_btn:MovieClip=new Btn_mc_undo();

var redo_btn:MovieClip=new Btn_mc_redo();

undo_btn.x=10;

undo_btn.y=350;

redo_btn.x=110;

redo_btn.y=undo_btn.y;

addChild(undo_btn);

addChild(container);

stage.addChild(redo_btn);

var nums:int;

undo_btn.addEventListener(MouseEvent.MOUSE_DOWN, undoEvent);

redo_btn.addEventListener(MouseEvent.MOUSE_DOWN, redoEvent);

function undoEvent(e:MouseEvent):void{

   

   

    container.removeChild(myArrObj.pop());

   

}

function redoEvent(e:MouseEvent):void{

    // nothing yet

}

var num:int=0;

function startDrawing(e:MouseEvent):void{

    shape=new Shape();

    container.addChild(shape);

   

    shape.name=String(num);

    myArr.push(shape.name);

    myArrObj.push(shape);

    shape.graphics.lineStyle(3, 0xcccccc);

    shape.graphics.moveTo(mouseX, mouseY);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, whileDrawing);

    num++;

   

}

function stopDrawing(e:MouseEvent):void{

    stage.removeEventListener(MouseEvent.MOUSE_MOVE, whileDrawing);

}

function whileDrawing(e:MouseEvent):void{

    shape.graphics.lineTo(mouseX, mouseY);

}

a couple of these lines are left over from troubleshooting (such as the num++);

This topic has been closed for replies.
Correct answer Ned Murphy

What I offered works.  Maybe you have something else going on in the file that you haven't accounted for.

If you put a trace(e.target) in your drawing function you will see it calls the drawing function when you click the button(s).  

Here's a link to a working sample file using your code with some adjustments for missing content...

http://www.nedwebs.com/Flash/AS3_Draw_Undo.fla

Another cure might be to create a separate drawing area from the buttons and target that area instead of the stage for your drawing listeners.

It would also be a good idea to use a CLICK listener for the buttons rather than a MOUSE_DOWN.

1 reply

Ned Murphy
Legend
August 23, 2013

Your undo is probably working flawlessly... the problem is that when you click the undo button you are effectively going thru the motions of drawing a new shape, adding a new one to the array, which is then removed when you release the button.

You might get around this by putting a conditional on the drawing code that excludes processing it when the event target is one of the buttons....

function startDrawing(e:MouseEvent):void{
   if(e.target != undo_btn){
        shape=new Shape();
        container.addChild(shape);

        shape.name=String(num);
        myArr.push(shape.name);
        myArrObj.push(shape);
        shape.graphics.lineStyle(3, 0xcccccc);
        shape.graphics.moveTo(mouseX, mouseY);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, whileDrawing);
        num++;
    }
}

function stopDrawing(e:MouseEvent):void{
   if(e.target != undo_btn){
       stage.removeEventListener(MouseEvent.MOUSE_MOVE, whileDrawing);
    }
}

markerline
Inspiring
August 23, 2013

Thanks for responding Ned.  This doesn't work.  I did read however after posting this that there is a solution for creating complex undos, meaning an undo that is beyond just one level deep.  The code above only goes down to one level then stops undoing.  I will research the solutions that I have found once they arrive in the mail.

-markerline

Ned Murphy
Ned MurphyCorrect answer
Legend
August 23, 2013

What I offered works.  Maybe you have something else going on in the file that you haven't accounted for.

If you put a trace(e.target) in your drawing function you will see it calls the drawing function when you click the button(s).  

Here's a link to a working sample file using your code with some adjustments for missing content...

http://www.nedwebs.com/Flash/AS3_Draw_Undo.fla

Another cure might be to create a separate drawing area from the buttons and target that area instead of the stage for your drawing listeners.

It would also be a good idea to use a CLICK listener for the buttons rather than a MOUSE_DOWN.