Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to increment and decrement a counter with one button

Guest
Jan 10, 2014 Jan 10, 2014

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!

TOPICS
ActionScript
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 10, 2014 Jan 10, 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
     mc

...
Translate
LEGEND ,
Jan 10, 2014 Jan 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 10, 2014 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 10, 2014 Jan 10, 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 11, 2014 Jan 11, 2014

Thanks Ned!

Using the count works perfect for flicking the visibility of 2 buttons but am struggling coming up with a solution for toggling multiple buttons.

I am trying to make a multiple choice scenario with 4 different buttons, where if any of the buttons are selected it turns the visibility of the other 3 buttons off. If you select the button again all the buttons are visible again and this can continue to be toggled.

To put it into context its kind of a reference sheet with answers that needs to be revisited and can be changed if necessary.

Here is the code I have so far, works great initially but when I return back to the scene all my buttons are invisible-

//Toe and Finger numbers

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

var RedF3Count:int;

var RedF4Count:int;

var RedF5Count:int;

//Red Toes and Fingers

//Red Fingers

//Red Finger number2

if ((RedF2Count%2 == 0)&&(RedF3Count%2 == 1)&&(RedF4Count%2 == 1)&&(RedF5Count%2 == 1)) {               // it's an even value 

    RedFing3.visible = true;

          RedFing4.visible = true;

          RedFing5.visible = true;

} else {                                // it's an odd value

     RedFing3.visible = false;

           RedFing4.visible = false;

           RedFing5.visible = false;

}

function RedF2(evt:MouseEvent):void {

     RedFing3.visible = !RedFing3.visible;

           RedFing4.visible = !RedFing4.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF2Count += 1;

           trace (RedF2Count);

}

RedFing2.addEventListener(MouseEvent.CLICK, RedF2);

//Red Fing number3

if((RedF3Count%2 == 0)&&(RedF2Count%2 == 1)&&(RedF4Count%2 == 1)&&(RedF5Count%2 == 1)) {               // it's an even value 

    RedFing2.visible = true;

          RedFing4.visible = true;

          RedFing5.visible = true;

} else {                                // it's an odd value

     RedFing2.visible = false;

           RedFing4.visible = false;

           RedFing5.visible = false;

}

function RedF3(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing4.visible = !RedFing4.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF3Count += 1;

           trace (RedF3Count);

}

RedFing3.addEventListener(MouseEvent.CLICK, RedF3);

//Red Fing number4

if((RedF4Count%2 == 0)&&(RedF2Count%2 == 1)&&(RedF3Count%2 == 1)&&(RedF5Count%2 == 1)) {               // it's an even value 

    RedFing2.visible = true;

          RedFing3.visible = true;

          RedFing5.visible = true;

} else {                                // it's an odd value

     RedFing2.visible = false;

           RedFing3.visible = false;

           RedFing5.visible = false;

}

function RedF4(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing3.visible = !RedFing3.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF4Count += 1;

}

RedFing4.addEventListener(MouseEvent.CLICK, RedF4);

//Red Fing number5

if((RedF5Count%2 == 0)&&(RedF2Count%2 == 1)&&(RedF3Count%2 == 1)&&(RedF4Count%2 == 1)) {               // it's an even value 

    RedFing2.visible = true;

          RedFing3.visible = true;

          RedFing4.visible = true;

} else {                                // it's an odd value

     RedFing2.visible = false;

           RedFing3.visible = false;

           RedFing4.visible = false;

}

function RedF5(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing3.visible = !RedFing3.visible;

           RedFing4.visible = !RedFing4.visible; // switch the visibility

          

     RedF5Count += 1;

}

RedFing5.addEventListener(MouseEvent.CLICK, RedF5);

if((RedF2Count == 0)&&(RedF3Count == 0)&&(RedF4Count == 0)&&(RedF5Count == 0)) {               // it's an even value 

    RedFing2.visible = true;

          RedFing3.visible = true;

          RedFing4.visible = true;

          RedFing5.visible = true;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 11, 2014 Jan 11, 2014
LATEST

Turns out I was over complicating it and my else statements where overwriting the previous function hence why they where all invisible when returned to.

Heres the code if anyone is interested -

//Toe and Finger counters

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

var RedF3Count:int;

var RedF4Count:int;

var RedF5Count:int;

//Red Fingers

//Red Finger number2

if ((RedF2Count%2 !== 0)&&(RedF3Count%2 == 0)&&(RedF4Count%2 == 0)&&(RedF5Count%2 == 0)) {               // it's an even value 

    RedFing3.visible = false;

           RedFing4.visible = false;

           RedFing5.visible = false;

}

function RedF2(evt:MouseEvent):void {

     RedFing3.visible = !RedFing3.visible;

           RedFing4.visible = !RedFing4.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF2Count += 1;

}

RedFing2.addEventListener(MouseEvent.CLICK, RedF2);

//Red Fing number3

if ((RedF3Count%2 !== 0)&&(RedF2Count%2 == 0)&&(RedF4Count%2 == 0)&&(RedF5Count%2 == 0)) {               // it's an even value 

    RedFing2.visible = false;

           RedFing4.visible = false;

           RedFing5.visible = false;

}

function RedF3(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing4.visible = !RedFing4.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF3Count += 1;

}

RedFing3.addEventListener(MouseEvent.CLICK, RedF3);

//Red Fing number4

if ((RedF4Count%2 !== 0)&&(RedF2Count%2 == 0)&&(RedF3Count%2 == 0)&&(RedF5Count%2 == 0)) {               // it's an even value 

   

          RedFing2.visible = false;

           RedFing3.visible = false;

           RedFing5.visible = false;

}

function RedF4(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing3.visible = !RedFing3.visible;

           RedFing5.visible = !RedFing5.visible; // switch the visibility

          

     RedF4Count += 1;

}

RedFing4.addEventListener(MouseEvent.CLICK, RedF4);

//Red Fing number5

if((RedF5Count%2 !== 0)&&(RedF2Count%2 == 0)&&(RedF3Count%2 == 0)&&(RedF4Count%2 == 0)) {               // it's an even value 

    RedFing2.visible = false;

           RedFing3.visible = false;

           RedFing4.visible = false;

}

function RedF5(evt:MouseEvent):void {

     RedFing2.visible = !RedFing2.visible;

           RedFing3.visible = !RedFing3.visible;

           RedFing4.visible = !RedFing4.visible; // switch the visibility

          

     RedF5Count += 1;

}

RedFing5.addEventListener(MouseEvent.CLICK, RedF5);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 10, 2014 Jan 10, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines