5 Replies Latest reply on Jul 18, 2006 9:23 AM by blemmo

    dynamic button action

    yogi_b
      I've created a dynamic set of buttons from an array that works fine. It also passes a variable in one function.
      My problem is that when I try to dim the picture (pix) on a button action it picks up the last [name] in the array and not the specific [name]. How does one pass the right item into the function? Thanks.


      function buildNewOverview() {
      var spacing:Number = 56;
      for (var i = 0; i < newProjects.length; ++i) {
      var name:String = "flipper" + i + "_mc";
      var x:Number = i * spacing;
      _root.overview.attachMovie("flipper", name, i);
      _root.overview[name]._x = x + 186;
      _root.overview[name].pix.loadMovie(newProjects [0]);
      _root.overview[name].movieID = newProjects
      [1];
      _root.overview[name].item_btn.onPress = function () {
      itemClicked (this._parent.movieID);
      _root.overview[name].pix._alpha = 30;

      }
      }
      }



      buildNewOverview();

      function itemClicked (movieID:String){
      loadMovie(movieID, "_root.project");
      }
        • 1. Re: dynamic button action
          blemmo Level 1
          It uses the value 'name' has at the time the button is clicked. This is when the loop is finished and 'name' resolves to the last item.
          You could use this._parent instead of _root.overview[name] like you did in the itemClicked assignment:

          _root.overview[name].item_btn.onPress = function () {
          itemClicked (this._parent.movieID);
          this._parent.pix._alpha = 30;
          }

          hth,
          blemmo
          • 2. Re: dynamic button action
            yogi bear Level 1
            hey blemmo,

            homerun. I knew it was in the parent but was using the wrong syntax/path.
            I was trying this._[name]pix._alpha and this._alpha and things like that but didn't have the exact syntax.

            If you have a minute, how would one then make the rest of the pixs in the array a different alpha. Would this need to be a conditional loop in the function?

            Thanks for your help,
            Yogi b
            • 3. Re: dynamic button action
              blemmo Level 1
              You mean all the other MCs on stage? This would need a loop, right. It should be quite the same loop as you used for the creation:

              _root.overview[name].item_btn.onPress = function () {
              itemClicked (this._parent.movieID);
              // assuming newProjects is in the _root
              for (var i = 0; i < newProjects.length; ++i) {
              _root.overview["flipper" + i + "_mc"].pix._alpha = 10;
              }
              this._parent.pix._alpha = 30;
              }

              It sets all pix to 10, then the selected pix to 30.
              • 4. Re: dynamic button action
                yogi bear Level 1
                blemmo

                homerun again. Once again I had the right idea with the syntax a little off. I'm a designer learning some programming.

                Any good books on this topic?

                Take care and thanks again,

                Yogi b
                • 5. Re: dynamic button action
                  blemmo Level 1
                  Colin Moock's "Essential Actionscript" books are very good. They also cover aspects of object oriented programming and how to do it in Flash. Maybe it's a bit too advanced for starters though... but well worth the money if you plan to get deeper into it.
                  Then there are a lot of good tutorials on the net: flashkit.com, kirupa.com, senocular.com, actionscript.org are some big sites (among lots of others), guess you can find a tutorial on almost anything there. I can't recommend a book for starters because I mainly used those to learn. There are good books out there for sure, but I guess you can also go with the online ressources. Maybe you can find book recommendations on those sites...

                  greets,
                  blemmo