8 Replies Latest reply on Jan 18, 2014 12:07 PM by Indiiyana

    How can I keep my counter the same when a button is clicked?

    Indiiyana

      Hi,

       

      I have the following button code:

      import flash.events.MouseEvent;

       

       

      var btns:Array = new Array(this.R1, this.R2, this.R3, this.R4);

       

       

      var currentlySelected:String = "";

      txtCounter.text = '3';

      var counter = 3;

       

       

      function clickedFN(e:MouseEvent):void

      {

                this.clearAllFN();

                e.target.gotoAndStop("on");

                subtractCount();

      }

       

       

      function subtractCount()

      {

                counter--;

                txtCounter.text = counter.toString();

      }

       

       

      function clearAllFN():void

      {

                for (var i:Number = 0; i < btns.length; i++)

                {

                          btns[i].gotoAndStop("off");

                }

      }

       

       

                for (var i:Number = 0; i < btns.length; i++)

                {

                          btns[i].addEventListener(MouseEvent.CLICK, clickedFN);

                }

       

      Basically, when the fourth button is selected I don't want anything to happen at all to the counter because the user had answered the question correctly.  How can I do this?  My code for the subtractCount to initalise is in the clickedFN function but I don't want that to happen to all of the buttons in my group as I only want that to happen to 3 of them.  I also tried the following IF statement within the function:

      function clickedFN(e:MouseEvent):void

      {

        this.clearAllFN();

        e.target.gotoAndStop("on");

        if (R1.selected || R2.selected || R3.selected == true)
        {

             subtractCount();
        }

        else if (R4.selected == true)
      {

            // do nothing
      }

      }

       

      Alas, it didn't work.

       

      Thanks.

        • 1. Re: How can I keep my counter the same when a button is clicked?
          John Waller Adobe Community Professional & MVP

          Which Adobe software are you using?

          • 2. Re: How can I keep my counter the same when a button is clicked?
            Indiiyana Level 1

            Woops, sorry.  I am using Flash.

            • 3. Re: How can I keep my counter the same when a button is clicked?
              badfl

              import flash.events.MouseEvent;

               

               

               

               

              var btns: Array = new Array(this.R1, this.R2, this.R3, this.R4);

               

               

               

               

              var currentlySelected: String = "";

              txtCounter.text = '3';

              var counter = 3;

               

               

               

               

              function clickedFN(e: MouseEvent): void {

                        if(e.currentTarget==btns[3])

                        {

                                  return;

                        }

                        this.clearAllFN();

                        e.target.gotoAndStop("on");

                        subtractCount();

              }

               

               

               

               

              function subtractCount() {

                        counter--;

                        trace(counter);

                        txtCounter.text = counter.toString();

              }

               

               

               

               

              function clearAllFN(): void {

                        for (var i: Number = 0; i < btns.length; i++) {

                                  btns[i].gotoAndStop("off");

                        }

              }

               

               

              for (var i: Number = 0; i < btns.length; i++) {

                        btns[i].addEventListener(MouseEvent.CLICK, clickedFN);

              }

              • 4. Re: How can I keep my counter the same when a button is clicked?
                Indiiyana Level 1

                Thanks man.

                 

                I have another question.  Is there a way to initalise that counter variable globally so that it's kept consistent throughout the program?  I have that code in each of my questions in my quiz and when the user clicks "Next" to go to the next question it resets itself to 3.

                • 5. Re: How can I keep my counter the same when a button is clicked?
                  badfl Level 1

                  Click next question when counter is set to 3

                  Example:

                   

                  import flash.events.MouseEvent;

                   

                   

                  nextBtn.addEventListener(MouseEvent.CLICK,nextClickHandler);

                  function nextClickHandler(event:MouseEvent):void

                  {

                            counter=3;

                  }

                  • 6. Re: How can I keep my counter the same when a button is clicked?
                    Indiiyana Level 1

                    Ah, I meant that when the next frame is shown, the counter is set itself back to 3, here's my code now for my radio group buttons:

                    import flash.events.MouseEvent;

                    import flash.media.Sound;

                     

                    var btns: Array = new Array(this.R1, this.R2, this.R3, this.R4);

                    var currentlySelected: String = "";

                    var myTimer:Timer = new Timer(5000, 1);

                    var snd:brba = new brba();

                    txtCounter.text = '3';

                    var counter = 3;

                     

                    function clickedFN(e: MouseEvent): void {

                              if(e.currentTarget==btns[0])

                              {

                                                            subtractCount();

                              }

                                          else if(e.currentTarget == btns[1])

                                          {

                                                    subtractCount();

                                          }

                                          else if(e.currentTarget == btns[2])

                                          {

                                                    subtractCount();

                                          }

                                          else if(e.currentTarget == btns[3])

                                          {

                                                    var delay:Number = 4000; //delay before caling myDelayFunction

                                                    setTimeout(gotoNext, delay);

                                                    snd.play();

                                                    delete this.R1();

                                          }

                                          this.clearAllFN();

                              e.target.gotoAndStop("on");

                                }

                     

                    function gotoNext()

                    {

                              MovieClip(root).gotoAndStop(4);

                    }

                     

                     

                     

                    function subtractCount()

                    {

                              counter--;

                              txtCounter.text = counter.toString();

                    }

                     

                    function clearAllFN(): void

                    {

                              for (var i: Number = 0; i < btns.length; i++) {

                                        btns[i].gotoAndStop("off");

                              }

                    }

                     

                     

                    for (var i: Number = 0; i < btns.length; i++) {

                              btns[i].addEventListener(MouseEvent.CLICK, clickedFN);

                    }

                     

                    Here's my second frame's radio group buttons' code:

                    import flash.events.MouseEvent;

                     

                    var btns: Array = new Array(this.R1, this.R2, this.R3, this.R4);

                    var currentlySelected: String = "";

                    txtCounter.text = '3';

                    var counter = 3;

                    var snd:miami = new miami();

                     

                     

                     

                     

                    function clickedFN(e: MouseEvent): void {

                              if(e.currentTarget==btns[1])

                              {

                                                            subtractCount();

                              }

                                          else if(e.currentTarget == btns[2])

                                          {

                                                    subtractCount();

                                          }

                                          else if(e.currentTarget == btns[3])

                                          {

                                                    subtractCount();

                                          }

                                          else if(e.currentTarget == btns[0])

                                          {

                                                    var delay:Number = 5000; //delay before caling myDelayFunction

                                                    setTimeout(gotoNext, delay);

                                                    snd.play();

                                          }

                              this.clearAllFN();

                              e.target.gotoAndStop("on");

                    }

                     

                     

                    function gotoNext()

                    {

                              MovieClip(root).gotoAndPlay(4);

                    }

                     

                     

                     

                     

                    function subtractCount() {

                              counter--;

                              txtCounter.text = counter.toString();

                    }

                     

                     

                     

                     

                    function clearAllFN(): void {

                              for (var i: Number = 0; i < btns.length; i++) {

                                        btns[i].gotoAndStop("off");

                              }

                    }

                     

                     

                    for (var i: Number = 0; i < btns.length; i++) {

                              btns[i].addEventListener(MouseEvent.CLICK, clickedFN);

                    }


                    See what I mean?  I've removed the "Next" button now because when the user clicks the correct answer it goes to the next frame after a delay of whatever I set it as.  But I don't want it to reset itself to 3 all of the time, I just want it to be set once and that is applied to every frame in the entire program that has "Lives" in it.  So it's set as 3 once, then when it goes to the next frame it may be 2 or 1 if the user clicked the wrong answer and when the user does get the correct answer it goes to the next frame and it would be 2 or 1 if that was how it was previously.

                    • 7. Re: How can I keep my counter the same when a button is clicked?
                      badfl Level 1

                      Do not understand what you mean,

                      When the first frame var counter = 3;

                      The second frame when you can not define counter

                      Only direct assignment on it, because the declared variables are global.

                      For example:

                      First frame:

                      stop ();

                      var counter = 3;

                       

                       

                       

                       

                      counter = 5;

                      this.gotoAndStop (2);

                      When the second frame

                      trace (counter); / / 5

                      • 8. Re: How can I keep my counter the same when a button is clicked?
                        Indiiyana Level 1

                        Perhaps this will illustrate what I want better:

                        http://puu.sh/6pvLf.png

                         

                        I tried doing the trace(counter) function but again the main problem is accessing the global variable in the first frame which is in a different timeline than the seperate radio group buttons.