7 Replies Latest reply on Jun 29, 2006 10:21 AM by MediaMan5335

    Loops

    MediaMan5335
      on the stage I have 9 movie clips with 9 buttons inside each. (these movie clips are going to be drag and drop)
      The movie clips are named 'answ1_mc' and buttons are named 'answ1_btn' (1-9). I am trying to put the code on the buttons using a loop but can't seem to get them to even trace something so far. I have never used a loop to put script on a button that is inside a movie clip before and don't know if it is possible. Here is my code so far, CAN SOMEONE PLEASE HELP ME:

      for (i=1; i<10; i++) {
      _root["answ"+i+"_mc"]._root["answ"+i+"_btn"].onPress = function() {
      trace("hello");
      };
      }
        • 1. Re: Loops
          Andra Veraart Level 1
          Hi there,

          Try this:

          for (i=1; i<10; i++) {
          _root.["answ"+i+"_mc"].["answ"+i+"_btn"].onPress = function() {
          trace("hello");
          };
          }

          The first _root wasn't seperated by a . from the answ1_mc, and the second _root shouldn't be there all together, as far as I know.
          I haven't tried it, but I think it'll work.

          Sometimes, I'll trace my targets just to check if I've named them correctly. In your case I would add:

          for (i=1; i<10; i++) {
          trace (_root.["answ"+i+"_mc"]);
          trace (_root.["answ"+i+"_mc"].["answ"+i+"_btn"]);
          _root.["answ"+i+"_mc"].["answ"+i+"_btn"].onPress = function() {
          trace("hello");
          };
          }

          If these traces come back as "undefined", you know you're not reaching the MC's and Button's you're trying to add the function to.

          Hope this helps!

          Yours,
          Andra


          • 2. Re: Loops
            MediaMan5335 Level 1
            Thank you for the effort, but I tried that and it didn't work, it come back with a sytax error. It doesn't like when there is a . inbetween the _root and the ["instanceName"+i] in a loop. However it does want a . before the _root["instanceName"+i] of the second part, to get to the level of the button in the movieClip. (it also wanted _roots at both parts). If anyone else knows anything please help, thanks.
            Here is the code again that I am trying to use in a loop to add code to buttons inside of movie clips.
            for(i = 1; i<9; i++){
            _root["answ"+i+"_mc"]._root["answ"+i+"_btn"].onPress = function(){
            trace("hello") //It won't trace this even.
            }
            ]
            • 3. Re: Loops
              Level 7
              > _root["answ"+i+"_mc"]._root["answ"+i+"_btn"].onPress = function() {

              That is just complete rubbish

              try

              _root["answ"+i+"_mc"]["answ"+i+"_btn"]
              --
              Jeckyl


              • 4. Re: Loops
                Level 7
                "Andra Veraart" <webforumsuser@macromedia.com> wrote in message
                news:e7ub0f$i1n$1@forums.macromedia.com...
                > Try this:
                >
                > for (i=1; i<10; i++) {
                > _root.["answ"+i+"_mc"].["answ"+i+"_btn"].onPress = function() {
                > trace("hello");
                > };
                > }

                That is also complete rubbish .. you've left the same mistakes in there as
                the original poster. Not terribly helpful.
                --
                Jeckyl


                • 5. Re: Loops
                  Andra Veraart Level 1
                  Sorry, I've never used array access operators to refer to movieclips before. Didn't know you had to leave out the dots.

                  Jeckyl: thanks for straightening us both out, but you kinda came across more like Mr. Hyde. Maybe I just misread things, but calling people's work "complete rubbish" just doesn't sound very nice.

                  Still, thanks again.

                  Yours,
                  Andra

                  • 6. Re: Loops
                    MediaMan5335 Level 1
                    I apreciate anyone who gives their input and effort to help, so thanks Andra and thanks for streightening us out Jackie.
                    Cheers!
                    • 7. Re: Loops
                      MediaMan5335 Level 1
                      ***This still returns an error***

                      for(i=1; i<6; i++){
                      _root["answ"+i"_mc"]["answ"+i+"_btn"].onPress=function(){
                      startDrag(this);
                      _root.x_var1 = _root.answ1_mc._x;
                      _root.y_var1 = _root.answ1_mc._y;
                      }
                      _root["answ"+i"_mc"]["answ"+i+"_btn"].onRelease=function(){
                      stopDrag();
                      if (this._droptarget == "/answ1_dz") {
                      _root.answ1_mc._x = _root.answ1_dz._x+30;
                      _root.answ1_mc._y = _root.answ1_dz._y+70;
                      _root.feedback = "Correct! Click the 'Next' button to continue.";
                      _root.verifyFinished_mc.gotoAndStop("correct");
                      _root.counter++;
                      } else {
                      _root.answ1_mc._x = _root.x_var1;
                      _root.answ1_mc._y = _root.y_var1;
                      }
                      }
                      }