8 Replies Latest reply on Feb 10, 2014 7:59 AM by Ned Murphy

    movieclips, arrays and for loops :(

    dannyb89

      Hi all, this is my current code and i will try to best explain whats happening.

       

      My images on the stage are named SC0_image, SC1_image etc and sit on the same timeline as my buttons which are named SC1, SC2 etc. i have managed to set the _alpha to 0 to start with. What i then wanted was for the images to fade in and out depending on which menu item (button) i hover over...this is proving to be a neusence.

       

      Any adivce would be much appriciated.

       

      var imageArray:Array = new Array();

       

      for (var i = 0; i < 7; i++)

      {

          this["SC" + i.toString() + "_image"]._alpha = 0;

      }

       

      function highlightMenuItems()

      {

          for (var i = 1; i < 7; i++)

          {

              this["SC" + i.toString()].onRollOver = function()

              {

                  var myTween:Tween = new Tween(this, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true); //works fine and fades my button out to display hover effect

                  var myTween:Tween = new Tween(this["SC" + i.toString() + "_image"], "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true); //not working

              };

              this["SC" + i.toString()].onRollOut = function()

              {

                  var myTween:Tween = new Tween(this, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                  var myTween:Tween = new Tween(this["SC" + i.toString() + "_image"], "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

              };

          }

      }

        • 1. Re: movieclips, arrays and for loops :(
          Ned Murphy Adobe Community Professional & MVP

          I don't see where you call the function that assigns the rollover/rollout actions.... highlightMenuItems()   You could include all of what's in that function in the first loop instead.

          • 2. Re: movieclips, arrays and for loops :(
            dannyb89 Level 1

            Sorry,  I have a button that gets clicked and animates an MC to a certain co-ordinate and once at that co-ordinate, calls the function. this button click function is on the timeline abover (_parent).

            • 3. Re: movieclips, arrays and for loops :(
              dannyb89 Level 1

              I think its to do with the structure of my timelines and where i am trying to call stuff

              • 4. Re: movieclips, arrays and for loops :(
                Ned Murphy Adobe Community Professional & MVP

                I cannot speak for how you have things coded versus the timeline.   From the code perspective I think it has to do with you using the same variable to define different tweens.  You should only be declaring a variable once.  If you assign something to that variable, and then immediately assign something else then the first assignment is voided. 

                1 person found this helpful
                • 5. Re: movieclips, arrays and for loops :(
                  dannyb89 Level 1

                  That makes sense. However i am still struggling. I basically want to write this as efficiently as possible:

                   

                  function highlightMenuItems()

                  {

                      SC1.onRollOver = function()

                      {

                          var myTween:Tween = new Tween(SC1, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);

                          var myTween:Tween = new Tween(SC1_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);

                   

                      };

                      SC1.onRollOut = function()

                      {

                          var myTween:Tween = new Tween(SC1, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                          var myTween:Tween = new Tween(SC1_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                      };

                      SC2.onRollOver = function()

                      {

                          var myTween:Tween = new Tween(SC2, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);

                          var myTween:Tween = new Tween(SC2_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);

                   

                      };

                      SC2.onRollOut = function()

                      {

                          var myTween:Tween = new Tween(SC2, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                          var myTween:Tween = new Tween(SC2_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                      };

                  }

                   

                  etc etc all the way to SC7 and SC7_hover. I thought loops and arrays would be ideal but obviously not as easy as i thought lol.

                   

                  cheers,

                  Dan

                  • 6. Re: movieclips, arrays and for loops :(
                    Ned Murphy Adobe Community Professional & MVP

                    It is not an issue of using the loops, it is an issue of coding the tweens....  example...

                     

                    SC2.onRollOut = function() 

                        {

                            var myTween:Tween = new Tween(SC2, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                            var myTween:Tween = new Tween(SC2_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                        };

                     

                     

                    That code just declared the same variable twice... a coding error.  There can only be one myTween variable declared.  But even if you were to declare that variable just once.... as in...

                     

                    SC2.onRollOut = function() 

                        {

                           var myTween:Tween;

                            myTween = new Tween(SC2, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                            myTween = new Tween(SC2_hover, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                        };

                     

                    there is still  the problem that myTween cannot be two different things at the same time.  Each tween should be assigned to a different variable.

                    1 person found this helpful
                    • 7. Re: movieclips, arrays and for loops :(
                      Ned Murphy Adobe Community Professional & MVP

                      Another problem you are probably encountering is mistargeting objects.  For example, in your rollover function...

                       

                      var myTween:Tween = new Tween(this, .... 

                      var myTween:Tween = new Tween(this["SC" + i.toString() + "_image"],...

                       

                      if "this" in the first line properly targeted your button or whatever it is, then it is also targeting your button in the second line.  Does your button contain the other object (SC#_image) ?  The code is trying to target something inside 'this'.

                       

                       

                      Another thing you have to be careful with is using a loop variable as a variable after the fact.  When you assign those functions you are assigning them as they are written - a variable to use is written in - it is not writing in the current value of the variable.  So the program is looking for the value of i when the function gets called.  That value will be the value obtained when the looping was complete (7).

                       

                      What you might try doing instead is to assign the i value as a property of the button and use that assigned value later on by calling upon it it from the button.  Or you could even assign the image object to the button as a property so that when you target the button for a tween, you can use its image property to target the image object associated with it.

                      • 8. Re: movieclips, arrays and for loops :(
                        Ned Murphy Adobe Community Professional & MVP

                        Here is a link to a sample file I made that demonstrates the few things I have tried to explain (probably poorly)...

                         

                        http://www.nedwebs.com/Flash/AS2_multitween.fla

                         

                        The file is created using CS6, just in case that is an issue for you.  It contains two sets of objects, each set being the button and its image (just simple mc's for this)

                         

                        The code used is as follows...

                         

                        import mx.transitions.Tween;
                        import mx.transitions.easing.Regular;

                         

                         

                        for (var i = 0; i < 2; i++)
                        {
                            this["SC" + i.toString() + "_image"]._alpha = 0;
                            this["SC" + i.toString()].ivalue = i.toString();  // assign the i value to the button
                            this["SC" + i.toString()].image = this["SC" + i.toString() + "_image"];  // assign the image object to the button
                        }

                         

                        // the code below uses both the ivalue and the image to show how each could be used

                        // the tweens are declared as different variables, myTween1 and myTween2


                        function highlightMenuItems()
                        {
                            for (var i = 0; i < 2; i++)
                            {
                                this["SC" + i.toString()].onRollOver = function()
                                {
                                    var myTween1:Tween = new Tween(this, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);


                                  // myTween2 uses the button's ivalue here

                                    var myTween2:Tween = new Tween(_level0["SC"+this["ivalue"]+"_image"], "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);
                                }

                                this["SC" + i.toString()].onRollOut = function()
                                {
                                    var myTween1:Tween = new Tween(this, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);

                         

                                    // myTween2 uses the button's image object here (could also target using ' this.image ')
                                    var myTween2:Tween = new Tween(this["image"], "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
                                }
                            }
                        }

                         

                        highlightMenuItems();

                        1 person found this helpful