14 Replies Latest reply on Apr 26, 2006 7:52 AM by kglad

    While loops & variables

    Soc4me3 Level 1
      Hi,
      I was having troubl writing some actionscript. My objective is to check all the letters in the alphabet if the letter is a then the movie clip for letter a will be played and so on. So if someone entered bob the movie clip for b should play then the mc for o then for b again. Here is the following while loop:

      _root.bike.stop();
      total = name.length
      answer = "";
      help = "";
      i = 0;

      while(i < name.length) {
      hello = name.toUpperCase().substring(i, i + 1);
      xpos = -65 + i * 15;
      if (hello eq "A") {
      setProperty("_root.MC.aLetter", _x, xpos);
      setProperty("_root.MC.aLetter", _y, 31.4);
      _root.MC.aLetter.play();
      answer = answer + "A";
      }
      if (hello eq "B"){
      setProperty("_root.MC.bLetter", _x, xpos);
      setProperty("_root.MC.bLetter", _y, 31.4);
      _root.MC.bLetter.play();
      answer = answer + "B";
      }
      if (hello eq "C"){
      setProperty("_root.MC.cLetter", _x, xpos);
      setProperty("_root.MC.cLetter", _y, 31.4);
      _root.MC.cLetter.play();
      }
      }
      stop();

      As you can see I never increase the value of i. This is because I need the "i++;" code in each letter movie clip. So in a frame in the movie clip for a will read "i++;". If anyone understands what I'm trying to ask I would appreciate some help. Sorry for it sounding confusing.
        • 1. Re: While loops &amp; variables
          kglad Adobe Community Professional & MVP
          is the user entering a name in an input textfield? if so, just use the onChanged textfield handler and play the movieclip of the just entered letter.
          • 2. Re: While loops &amp; variables
            Soc4me3 Level 1
            The user is entering text on frame 1 and on frame 291 the text is being used so if the first letter of the input text was a then a movie clip for the letter a will be played. If the second letter was b i want the movie clip for the letter B to play AFTER the letter A.
            • 3. Re: While loops &amp; variables
              kglad Adobe Community Professional & MVP
              do you want to stagger the playing of these movieclips or do you want them to play at the same time? if you want to stagger them by timeVar milliseconds and if the user entered their text in a textfield with instance name tf, you can use:
              • 4. Re: While loops &amp; variables
                Soc4me3 Level 1
                Thank you for your help. I need them staggered eventually. Right now I'm reading in the text and playing a movie clip according to which letter it is. So if the input is "ABC", it should play the MC for A then go back and play the MC for B and so on. I decided to use the while method so while my variable is less than the number of characters in the input the code continues. Inside the while loop i have if loops to check which letter it is. I was increasing the variable of the while loop in these movie clips for each letter. It didn't seem to work and I'm not sure why.
                • 5. Re: While loops &amp; variables
                  Soc4me3 Level 1
                  Thank you so much. Your code was much more efficient however I am unfamiliar with:
                  quote:

                  _root.MC[tf.text.charAt(tfIndex).toLowerCase()+"Letter"].play();

                  What is the MC and the tf before text do? Again I am very new to actionscript and just need a few pieces to finish an animation. Thank you for all your help
                  • 6. Re: While loops &amp; variables
                    kglad Adobe Community Professional & MVP
                    _root.MC is your movieclip that contains your letter movieclips. that's the instance name you used in your above message.

                    tf is the instance name of your input textfield, as i mentioned in my above message.

                    that complete line (_root.MC[tf.text.charAt(tfIndex).toLowerCase()+"Letter"].play(); ) plays your letter movieclips, one after the other by going through the text in the input textfield, extracting the letters that were entered and then instructing the appropriate letter movieclip to play.
                    • 7. Re: While loops &amp; variables
                      Soc4me3 Level 1
                      Thank you so much. I made a stupid mistake. The code you wrote was much more efficient. I have one more question. With the code you helped me finish, it plays a movie clip according to the letter. However there becomes a problem if someone enters "aa" because the second movie clip doesn't play. Is there a way to fix this when someone enters double letters? Thank you
                      • 8. Re: While loops &amp; variables
                        kglad Adobe Community Professional & MVP
                        any problem with repeated letters is a result of how you bring that letters to the stage and not the code i gave.

                        there's nothing in any of the posted messages that indicates how the letter movieclips are instantiated. so, i'm not sure how to repair your the problem with your current set-up.

                        i would use attachMovie() to bring the movieclips to the stage and that would resolve any problem with duplicate letters betting entered.
                        • 9. Re: While loops &amp; variables
                          Soc4me3 Level 1
                          The way I was instantiating was by making a blank key frame in the MC and then dragging it onto the stage and giving it an instance name of (EX: aLetter).
                          • 10. Re: While loops &amp; variables
                            kglad Adobe Community Professional & MVP
                            and that's going to be a problem. you could drag multiple copies of each letter movieclip and give them different instance names, but that's not going to be as elegant nor as easy to control as attaching the movieclips from the library.

                            remove the movieclips from the stage and give them linkage id's (that match their current instance names). you can then use:
                            • 11. Re: While loops &amp; variables
                              Soc4me3 Level 1
                              I have removed all the movie clips from on the stage and changed the properties or linkage so that they read the instance names. I then copied your code in and it stopped playing the movie clips. Any ideas?
                              • 12. Re: While loops &amp; variables
                                kglad Adobe Community Professional & MVP
                                to define a linkage id you need to right click on your movieclips in the library, left click linkage, tick export for actionscript, enter aLetter, bLetter, cLetter etc for the id name and click ok.

                                if you did that and are still having problems, post a link to your fla.
                                • 13. Re: While loops &amp; variables
                                  Soc4me3 Level 1
                                  First I want to say you have been really helpful with my project. I haven't tried it yet because I was working on another part of the project but I will let you know how it goes. Thanks
                                  • 14. Re: While loops &amp; variables
                                    kglad Adobe Community Professional & MVP
                                    you're welcome. and good luck!