Skip to main content
January 10, 2014
Answered

How to increment and decrement a counter with one button

  • January 10, 2014
  • 6 replies
  • 3044 views

Is there anyway you can increment and decrement a counter using only one button?

I want this counter in place so that when I leave the scene and return back to it the visibility of my button will stay the same due to being linked to counter number.

What would be the best way to implement this as it does not work when I do it this way-

function show_hide_MC(evt:MouseEvent):void {

     mc.visible = !mc.visible;

     (Counter = 0) = (Counter = 1);

}

btn.addEventListener(MouseEvent.CLICK, show_hide_MC);

Thanks in advance!

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Ned Murphy

You could use a Boolean (true/false) rather than a counter for keeping track/assigning the visibility.  You could use the counter simply to determine whether you are just starting the file or not.   When you click that button you set the mc.visible property and then assign the boolean the visible property of the mc.

var mcVisible:Boolean;    // these two variables need to extend the length off the timeline
var count:int;

if(count == 0) {                // you've just started so set things up
     mcVisible = mc.visible;
     count += 1;
} else {
     mc.visible = mcVisible; // you're returning so set the visibility of the mc
}

function show_hide_MC(evt:MouseEvent):void {
     mc.visible = !mc.visible; // switch the visibility
     mcVisible = mc.visible;    // remember how you left it
}

You could just use the count as well, incrementing it everytime you click the button and using the modulus to determine if it has been clicked an even number of times versus an odd number of times and then have a conditional that tests that and assigns the visibility... something like...

var count:int;           // will start off with a default of 0

                                // needs to extend the length of the timeline

if(count%2 == 0) {               // it's an even value 
    mc.visible = true;
} else {                                // it's an odd value
     mc.visible = false;

}

function show_hide_MC(evt:MouseEvent):void {
     mc.visible = !mc.visible; // switch the visibility
     count += 1;

}

6 replies

Inspiring
January 11, 2014

I am not sure I grasp entire use case but you can just cast Boolean to int:

function show_hide_MC(evt:MouseEvent):void

{

          mc.visible = !mc.visible;

          Counter = int(mc.visible);

}

btn.addEventListener(MouseEvent.CLICK, show_hide_MC);

Ned Murphy
Legend
January 10, 2014

In what way is the button supposed to link to the counter?  How does using the button affect the counter?  How does the counter affect the button?

January 10, 2014

counter is supposed to be 0 when visible and 1 when invisible.

So that when the button is clicked it changes between the two.

I was told that using a counter was the best way to save the state of your movie clips so that when you move from a scene then return back to it the scene will be in the same state as you left it by refering to the counter number.

Hope that makes sense.

Ned Murphy
Ned MurphyCorrect answer
Legend
January 11, 2014

You could use a Boolean (true/false) rather than a counter for keeping track/assigning the visibility.  You could use the counter simply to determine whether you are just starting the file or not.   When you click that button you set the mc.visible property and then assign the boolean the visible property of the mc.

var mcVisible:Boolean;    // these two variables need to extend the length off the timeline
var count:int;

if(count == 0) {                // you've just started so set things up
     mcVisible = mc.visible;
     count += 1;
} else {
     mc.visible = mcVisible; // you're returning so set the visibility of the mc
}

function show_hide_MC(evt:MouseEvent):void {
     mc.visible = !mc.visible; // switch the visibility
     mcVisible = mc.visible;    // remember how you left it
}

You could just use the count as well, incrementing it everytime you click the button and using the modulus to determine if it has been clicked an even number of times versus an odd number of times and then have a conditional that tests that and assigns the visibility... something like...

var count:int;           // will start off with a default of 0

                                // needs to extend the length of the timeline

if(count%2 == 0) {               // it's an even value 
    mc.visible = true;
} else {                                // it's an odd value
     mc.visible = false;

}

function show_hide_MC(evt:MouseEvent):void {
     mc.visible = !mc.visible; // switch the visibility
     count += 1;

}