3 Replies Latest reply on Apr 5, 2012 5:33 PM by kglad

    Help With Button Coding

    ilovetoread247

      I make dress up games in ActionScript 2, and am having a problem.

       

      I want to make a button find all movieclip items with, say, "shirt" in their Instance Name, and move them to frame 2.  I know how to do this with one item, (on(release){

      _root.shirt1.gotoAndStop(1);

      }

      and on previous games, I merely copied and pasted that several dozen times for every item.  However, on this game, I am trying to cut down on code, and learned how to use

       

      for(var i:String in _root){

           if(i.indexOf("_drag") > -1){

                _root[i].onPress = function(){

       

      (put in the code)

      (put in the closing }s)

       

      on the stage to make items drag and snap into place.  I have learned that this code cannot be used on buttons, which need static commands, I think?  I'm still very much a novice.

       

      Any help would be appreciated!

        • 1. Re: Help With Button Coding
          kglad Adobe Community Professional & MVP

          what is it that, you think, doesn't work with buttons?

          • 2. Re: Help With Button Coding
            ilovetoread247 Level 1

            I discovered my mistake- kind of (I had forgotten to indent).  However, I now have two related problems.

             

            One: the code I am using is

             

            on(release){

                 for(var i:String in _root){

                      if(i.indexOf("shirt") > -1){

                           _root[i].onPress = function(){

                                this.gotoAndStop(2);

                           }

                      }

                 };

            }

             

            the onPress here refers to the item- I have to press the button, then the item, for it to move to the next frame.  I don't like this, and want the button to do all the work for me.  How do I do this?

             

            Secondly, this overrides the original code on a frame of the stage, given above, and the items no longer drag.  This is obviously not what I want.  How do I make sure the items are "hearing" both codes?

             

            Thank you, and sorry.

            • 3. Re: Help With Button Coding
              kglad Adobe Community Professional & MVP

              you should remove all code from objects.  so, whatever button has that code attached should be given an instance name (eg, btn) so you can use:

               

              btn.onRelease = function(){

                   for(var i:String in _root){

                        if(i.indexOf("shirt") > -1){

                             _root[i].gotoAndStop(2);

                             }

                        }

                   };

              }