6 Replies Latest reply on Sep 21, 2009 2:31 AM by AESS1981

    track pIndex value in array

    AESS1981

      I am using an array with a pIndex value of 0:

       

      this.pArray = ["1.swf", "2.swf", "3.swf", "4.swf"];

      this.pIndex = 0;

       

       

      Each time a button is pressed, the array number increases by 1:

       

      MovieClip.prototype.changeImage = function(d) {

      this.pIndex = (this.pIndex+d)%this.pArray.length;

      if (this.pIndex<0) {

      this.pIndex += this.pArray.length;

      }

       

       

      I am trying to determine the pIndex value within the array, then use an 'if' statement to make certain mcs visible (depending on the value of pIndex). The following does not seem to work;

       

      if (this.pIndex = (this.pIndex=0)%this.pArray.length){

      menu1_wht._visible = true

      menu2_wht._visible = false

      menu3_wht._visible = false

      menu4_wht._visible = false

      }

       

      I have also tried:

       

      if (this.pIndex =0) ...

       

      if (this.pArray[0]) ...

       

       

      Where am I going wrong?

      Thanks a lot

        • 1. Re: track pIndex value in array
          Ned Murphy Adobe Community Professional & MVP

          = is for assigning values.  When you are comparing values for equality in a conditional statement you use ==.

           

          And just for the record I still haven't wrapped my head around what is going on with this line of code:  (this.pIndex=0)%this.pArray.length

           

           

          You shouldn't have to determine the pIndex value in the array, it should be whatever value it is currently assigned, so all you probably need is...

           

          if (this.pIndex == 0){

          menu1_wht._visible = true

          menu2_wht._visible = false

          menu3_wht._visible = false

          menu4_wht._visible = false

          }

          • 2. Re: track pIndex value in array
            AESS1981 Level 1

            Thanks Ned - this works initially.

             

            When the user loads a new image from the array, the pIndex number changes.

            What I cannot achieve is:

            Every time the pIndex number changes I need a different mc to become visible. I have tried:

             

            if (this.pIndex == 0){

            menu1_wht._visible = false

            menu2_wht._visible = true

            menu3_wht._visible = false

            menu4_wht._visible = true

            }

            else if (this.pIndex == 1){

            menu1_wht._visible = false

            menu2_wht._visible = true

            menu3_wht._visible = false

            menu4_wht._visible = false

            }

            else if (this.pIndex == 2){

            menu1_wht._visible = false

            menu2_wht._visible = false

            menu3_wht._visible = true

            menu4_wht._visible = false

            }

            else if (this.pIndex == 3){

            menu1_wht._visible = false

            menu2_wht._visible = false

            menu3_wht._visible = false

            menu4_wht._visible = true

            }

            I don't think the code traces the changing pIndex value.
            Is this possible?
            Thanks

            • 3. Re: track pIndex value in array
              Ned Murphy Adobe Community Professional & MVP

              I don't know why you are defining var using 'this', you should probably use var instead....

               

              var pArray = ["1.swf", "2.swf", "3.swf", "4.swf"];

              var pIndex = 0;

               

              If you don't think the code traces the pIndex value, just try tracing it and see.  Knowing what it is tracing will tell more than thinking it doesn't.

               

              Also, assuming the first set you show has one true too many, rather than the series of conditionals that you have, you can reduce that to the following....

               

              for(i=1; i<5; i++){

                   this["menu"+(i+1)+"_wht"]._visible = false; // hide all

              }

              this["menu"+(pIndex+1)+"_wht"]._visible = true; // show the one

              1 person found this helpful
              • 4. Re: track pIndex value in array
                AESS1981 Level 1

                You are right, none of my code needed '.this'. I have removed all references to help tidy code.

                I love the simplicity of the code you have suggested for the mcs being visible or invisible but am still having issues at the last hurdle.

                 

                The full code is below with the problem area in red bold. I have annotated as much as poss to assist. If you are able to look at it would be much appreciated:

                 

                 

                 

                // Number menus default at invisible until associated image has loaded. //

                 

                menu1.gotoAndStop("invisible");

                menu2.gotoAndStop("invisible");

                menu3.gotoAndStop("invisible");

                menu4.gotoAndStop("invisible");

                 

                 

                var pathToPics = "portfolio/100_design/";

                var pArray = ["1.swf", "2.swf", "3.swf", "4.swf"];

                var fadeSpeedOut = 40;

                var fadeSpeedIn = 20;

                var pIndex = 0;

                 

                 

                // '_global.portfoliostate' tracks playhead position in timeline. //

                // Change the pArray[] value to target the relevant image. //

                // Change the pIndex= value to determine the position within the array. //

                 

                if (_global.portfoliostate == "2_brand_identity"){

                loadMovie(pathToPics+pArray[1], _root.image_holder);

                pIndex = (pIndex=1)%pArray.length;

                }

                else if (_global.portfoliostate == "3_promotional"){

                loadMovie(pathToPics+pArray[2], _root.image_holder);

                pIndex = (pIndex=2)%pArray.length;

                }

                else if (_global.portfoliostate == "4_self_promotional"){

                loadMovie(pathToPics+pArray[3], _root.image_holder);

                pIndex = (pIndex=3)%pArray.length;

                }

                else {

                loadMovie(pathToPics+pArray[0], _root.image_holder);

                }

                 

                // Individual functions for number buttons. //

                // Change the pIndex= value to determine the position within the array. //

                 

                MovieClip.prototype.menuOne = function(d) {

                pIndex = (pIndex=0)%pArray.length;

                onEnterFrame = fadeOut;

                };

                MovieClip.prototype.menuTwo = function(d) {

                pIndex = (pIndex=1)%pArray.length;

                onEnterFrame = fadeOut;

                };

                MovieClip.prototype.menuThree = function(d) {

                pIndex = (pIndex=2)%pArray.length;

                onEnterFrame = fadeOut;

                };

                MovieClip.prototype.menuFour = function(d) {

                pIndex = (pIndex=3)%pArray.length;

                onEnterFrame = fadeOut;

                };

                 

                 

                // Functions for loading swfs into image gallery and controlling their behavior. //

                 

                MovieClip.prototype.changeImage = function(d) {

                // make sure pIndex falls within pArray.length

                pIndex = (pIndex+d)%pArray.length;

                if (pIndex<0) {

                pIndex += pArray.length;

                }

                onEnterFrame = fadeOut;

                };

                MovieClip.prototype.fadeOut = function() {

                if (image_holder._alpha>fadeSpeedOut) {

                image_holder._alpha -= fadeSpeedOut;

                } else {

                loadPhoto();

                }

                };

                MovieClip.prototype.loadPhoto = function() {

                // specify the movieclip to load images into

                p = _root.image_holder;

                p._alpha = 0;

                trace(pathToPics+pArray[pIndex]);

                p.loadMovie(pathToPics+pArray[pIndex]);

                onEnterFrame = loadMeter;

                };

                MovieClip.prototype.loadMeter = function() {

                i, l, t;

                l = image_holder.getBytesLoaded();

                t = image_holder.getBytesTotal();

                if (t>0 && t == l) {

                onEnterFrame = fadeIn;

                } else {

                trace(l/t);

                }

                };

                MovieClip.prototype.fadeIn = function() {

                if (image_holder._alpha<100-fadeSpeedIn) {

                image_holder._alpha += fadeSpeedIn;

                } else {

                image_holder._alpha = 100;

                onEnterFrame = null;

                }

                };

                 

                 

                // Track the pIndex value and make the relevant mc visible. //

                 

                for(i=1; i<5; i++){

                ["menu"+(i+1)+"_wht"]_visible = false; // hide all

                }

                ["menu"+(pIndex+1)+"_wht"]_visible = true; // show the one

                 

                 

                 

                 

                ends.

                 

                Thanks so much for your efforts.

                • 5. Re: track pIndex value in array
                  Ned Murphy Adobe Community Professional & MVP

                  In the example code I gave, "this" is meaningful to include, though you could use _root if that is applicable as well I suppose (I generally avoid _root references).

                   

                  The way you have shown the code I have to assume is the way you have it coded in your file.  I would have thought that red code you highlighted would be within some function that responds to some action that changed the pIndex value, such as a button function.  Sitting at the end of the code as you have it, it will only execute one time when that frame is entered.

                  1 person found this helpful
                  • 6. Re: track pIndex value in array
                    AESS1981 Level 1

                    Hey,

                     

                    That makes sense what you have stated.

                     

                    Unfortunately I am still struggling. On my 'next' button that loads the next swf I have:

                     

                    on (release){

                    _root.changeImage(1);

                    for(i=1; i<5; i++){

                    _root["menu"+(i+1)+"_wht"]._visible = false; // hide all

                    }

                    _root["menu"+(pIndex+1)+"_wht"]._visible = true; // show the one


                    }

                     

                     

                    '_root.changeImage(1);' triggers the image change successfully but the visible / invisible doesn't seem to work.

                    I understand theoretically how it should work now, but am I missing anything?

                    Thank you