Using Variables in AS3

I am trying to have my timeline play from different frames, depending what frame it is currently on when the button is pressed.

So Pressing the button 3 times will play:
A->B
B->A
A->B

But instead it plays
A->B
A->B
B->A

after the first A->B the variable “varButtonFrame” is set to 2, like it should be. but then is reset to “Undefined”. but after this it works as it should. I assume i must have the “var varButtonFrame:int;” in the wrong place…? but not sure where else i can put it.

This is code in a movieclip within the scene:

var varButtonFrame:int;

//============Buttons========================
Menu_B_2D.addEventListener(MouseEvent.CLICK, Menu_Trans_2D);


 
function Menu_Trans_2D(event:MouseEvent):void 
{
    //MovieClip(this).gotoAndStop("2");

        trace("start of if" + varButtonFrame);
        ///////////////////////////////////////////////
        if(varButtonFrame!=2)
{
    varButtonFrame = 1;
}

trace("the 2nd is" + varButtonFrame);

///////////////////////////////////////////////////
        
    if(varButtonFrame==1)
    {
        varButtonFrame = 3;
        MovieClip(root).gotoAndPlay("890");

    }
    if(varButtonFrame==2)
    {
        varButtonFrame = 1;
        MovieClip(root).gotoAndPlay("941");
    }
    if(varButtonFrame==3)
    {
        varButtonFrame = 2;
    }
    
    trace(varButtonFrame);
}