3 Replies Latest reply on Mar 1, 2012 7:25 AM by kglad

    how would you write a code to automate this??

    subtlefly72

      Hi team,

      Still trying to improve my coding..

      So I have got all of these buttons... how would I go about coding so I dont have this function repeated 100 times??

      Cheers,

      Sub

      e.g.

      GSmain.gsshape.gss1.gssr.gssr1.addEventListener(MouseEvent.CLICK, page2go)

      function page2go(e:MouseEvent){

                gotoAndStop(2);

      }

      GSmain.gsshape.gss1.gssr.gssr2.addEventListener(MouseEvent.CLICK, page3go)

      function page3go(e:MouseEvent){

                gotoAndStop(3);

      }

      GSmain.gsshape.gss1.gssr.gssr3.addEventListener(MouseEvent.CLICK, page3go)

      function page3go(e:MouseEvent){

                gotoAndStop(4);

      }

      etc etc

        • 1. Re: how would you write a code to automate this??
          kglad Adobe Community Professional & MVP

          use:

           

           

          for(var i:int=1;i<=100;i++){

          GSmain.gsshape.gss1.gssr["gssr"+i].addEventListener(MouseEvent.CLICK, pagego)

          }

           

          function pagego(e:MouseEvent){

                    gotoAndStop(int(e.currentTarget.name.substring(4)));

          }

          • 2. Re: how would you write a code to automate this??
            subtlefly72 Level 1

            hey kglad thanks for this, I will use it, but I would like to know how it is working..  what is going on with the substring?  What is that?

            Cheers for your help,

            You are a champion

            • 3. Re: how would you write a code to automate this??
              kglad Adobe Community Professional & MVP

               

              // loop from 1 to 100
              for(var i:int=1;i<=100;i++){
              // use array notation to coerce flash to convert strings to objects

              GSmain.gsshape.gss1.gssr["gssr"+i].addEventListener(MouseEvent.CLICK, pagego)

              }

               

              function pagego(e:MouseEvent){

              // gssr1,gssr2,etc are all currentTarget event dispatchers. one of them called this function.  to find which, use the flash string methods.

              // e.currentTarget.name is a string like "gssr1","gssr2",...,"gssr100"

              // the String.substring(n) method returns a substring of String starting at index n.

              // so e.currentTarget.name.substring(4) is "1","2",...,"100" depending on which object was clicked

              // int("n"), converts a string to an int for use in the gotoAndStop() method:

                        gotoAndStop(int(e.currentTarget.name.substring(4)));

              }