5 Replies Latest reply on Nov 22, 2013 9:30 AM by kglad

    movieclips, buttons and arrays  - oh the joys

    DMB89

      I am having such an issue working with arrays, im not great at fpash but decided to try a different approach to writing out the same functions for different buttons over and over again.

       

      The plan is meant to be:

       

      6 movieclip buttons on stage (markers) and 6 movieclip boxes on stage (panels). click a marker and it plays the fade in animation within the panel.

      access/call the close button.onRelease function; location within the animated movieclip of the original panel movieclip (p1.p1_an.close_btn) to then call rewindframes(); to fade out the panel back to square one again.

       

      repeat for all 6 buttons and clips.

       

      Here is my code. i know its probably got something to do with strings and converting values to objects etc but i have tried so many things i just cannot get it to work.

       

      please help

       

      stop();
      
      var myMarker = new Array (marker1, marker2, marker3, marker4, marker5, marker6);
      var myPanel = new Array(p1, p2, p3, p4, p5, p6);
      
      //***************************//
      // Markers buttons
      //***************************//
      
      for (var i = 1; i < myMarker.length; i++)
      {
          myMarker[i].onRelease = function()
          {
              play();//fadeout markers
              
              playPanel(myPanel[i]); // play panel 1 to 6 depending on marker clicked    
              
              this[myPanel[i] + "." myPanel[i] + "_an"].close_btn.onRelease = function() //location of close button example - p1.p1_an.close_btn
              {
                  rewindFrames(myPanel[i]); // function to rewind frames of panel fade in animation back to frame 0;
              };
          };
      }
      
      //***************************//
      // Play Panel
      //***************************//
      
      function playPanel(panelName)
      {
          panelName.play();
      }
      
      //***************************//
      // Rewind Panel
      //***************************//
      
      function rewindFrames(panelName)
      {
          panelName.onEnterFrame = function()
          {
              if (panelName._currentframe != 1)
              {
                  panelName.prevFrame();
              }
              else
              {
                  panelName.stop();
                  delete this["onEnterFrame"];
              }
          };
      }
      
      //***************************//
      // Fade in Markers
      //***************************//
      
      function fadeInMarkers()
      {
          onEnterFrame = function ()
          {
              if (_currentframe != 30)
              {
                  prevFrame();
              }
              else
              {
                  stop();
                  delete this["onEnterFrame"];
              }
          };
      }
      
        • 1. Re: movieclips, buttons and arrays  - oh the joys
          kglad Adobe Community Professional & MVP

          use:

           

          stop();
          
          var myMarker = new Array (marker1, marker2, marker3, marker4, marker5, marker6);
          var myPanel = new Array(p1, p2, p3, p4, p5, p6);
          
          //***************************//
          // Markers buttons
          //***************************//
          
          for (var i = 1; i < myMarker.length; i++)
          {
          myMarker[i].ivar=i;
               myMarker[i].onRelease = function()     {         play();//fadeout markers                 playPanel(myPanel[this.ivar]); // play panel 1 to 6 depending on marker clicked   
          // the line below looks screwy. but if there really is a p1.p1, p2.p2 etc this is one way to encode it

          myPanel[this.ivar][myPanel[this.ivar]._name].close_btn
          .onRelease = function() //location of close button example - p1.p1_an.close_btn         {             rewindFrames(this._parent._parent); // function to rewind frames of panel fade in animation back to frame 0;         };     }; }
          • 2. Re: movieclips, buttons and arrays  - oh the joys
            DMB89 Level 1

            thankyou very much, this has good me alot further already. the line below is meant to convert to the following depending on the marker clicked. you are right it doent work but everything else did:

             

            p1.p1_an.close_btn.onRelease = function()

            p2.p2_an.close_btn.onRelease = function()

            p3.p3_an.close_btn.onRelease = function()

            p4.p4_an.close_btn.onRelease = function()

            p5.p5_an.close_btn.onRelease = function()

            p6.p6_an.close_btn.onRelease = function()

             

            // the line below looks screwy.  i'm not sure what it's supposed to do but it's not likely to work.       
                    this[myPanel[this.ivar] + "." myPanel[this.ivar] + "_an"].close_btn.onRelease = function() //location of close button example - p1.p1_an.close_btn

            • 3. Re: movieclips, buttons and arrays  - oh the joys
              kglad Adobe Community Professional & MVP

              check my updated message.  i think i figured out what you're trying to do.

              • 4. Re: movieclips, buttons and arrays  - oh the joys
                DMB89 Level 1

                PERFECT. works a treat. thank you again so much.

                 

                can you brifly explain how adding .ivar and converting all into [i] works? im a bit confused about that.

                • 5. Re: movieclips, buttons and arrays  - oh the joys
                  kglad Adobe Community Professional & MVP

                  when your onRelease functions execute your for-loop has long finished looping and i is equal to myMarker.length.  ie,

                   

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

                  }

                  trace(i);  // should be myMarker.length

                   

                  so, each myMarker[i] element needs to know which value of i was used when its onRelease was defined.  that value is stored in ivar (myMarker[i].ivar=i).  then when the onRelease executes it uses the triggering object's ivar value to retrieve the value of i when the onRelease was defined.