7 Replies Latest reply on Apr 4, 2007 7:56 AM by Beppo5

    Trying to save key strokes with a for loop

    Beppo5
      I have been trying to action script for years with no success. I to some time off and learned javascript and some oop (ruby) so I thought I would take another stab at it. I am still having problems. Here goes...

      I have several movieclips on the stage that will be set up as buttons. They all have the same effects associated with mouseover and mouseout so I thought I could save some key strokes with a for loop. But I can't get it working right. Could someone tell me what I am doing wrong please?


      If anyone can help it would be appreciated.
        • 1. Re: Trying to save key strokes with a for loop
          kglad Adobe Community Professional & MVP
          those declarations at the top of your for-loop aren't doing anything useful. what are the names of you movieclips that are on-stage?
          • 2. Re: Trying to save key strokes with a for loop
            Beppo5 Level 1
            kglad,
            Thanks for taking a look. In answer to your question. The movie clip instance names are mc_ItemText0 ... mc_ItemText3.
            quote:

            those declarations at the top of your for-loop aren't doing anything useful. what are the names of you movieclips that are on-stage?


            In reg\ards to the variable declarations, sorry my variable names are causing confusion. I use the name "mc" for two different variables. Once for the FOR loop and a separate time locally in the functions grow and shrink. The ones set for the For loop are used in the 'onEvents' at the bottom of the script. Sorry for the confusion.
            Because the code works if I take it out of the FOR loop and replace the 's with the numbers that corruspond to the movie clip; I don't think the similar names are conflicting because of the scope. To make the code more readable and eliminate confusion I have posted the code with unique variable names.

            I have searched the forum for FOR loop difficulties and found several cases that seemed similar to mine, but I didn't understand what was happening.
            Examples:
            http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid =1253110&highlight_key=y&keyword1=for%20loop
            http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid =1202338&highlight_key=y&keyword1=for%20loop

            My code with unique variable names to end confusion:

            • 4. Re: Trying to save key strokes with a for loop
              Beppo5 Level 1
              When I run your code I get two errors:
              "
              **Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 2: Syntax error.
              mc = this["mc_ItemText"+i];

              **Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 11: Unexpected '}' encountered
              }
              "
              I am sure it has to do with how I have things set up. I am having problems trouble shooting the issue because I don't understanding everything that is going on.
              Namely the "this" construction. what is the "this" keyword referring to? the for loop?:
              mc = this["mc_ItemText"+i];

              How is this supposed to work?
              also why is it having issues with the '}' in line 11

              for (var i = 0; i<4; i++) {
              mc = this["mc_ItemText"+i];
              mc.onRollOver = function():Void {
              clearInterval(this.int1);
              this.int1 = setInterval(grow, 10, this);
              };
              mc.onRollOut = function():Void {
              clearInterval(this.int1);
              this.int1 = setInterval(shrink, 10, this);
              };
              } <-- this is the '}' in line 11. It closes the for loop.

              Any advice as to how to move forward... My deadline looms ominously. Yikes!
              • 5. Re: Trying to save key strokes with a for loop
                kglad Adobe Community Professional & MVP
                there's no syntax error and i can't think of anything you could do that would cause that code to generate a syntax error.

                try creating a new fla and paste all frame 1 layers into the new fla. retest. if it works ok just copy the rest of your main timeline to the new main timeline and continue your project in the new fla. the original fla may have become corrupted.

                p.s. "this" on line 2 refers to the timeline that contains that code. the "this" in this.int1 refers to mc.
                • 6. Re: Trying to save key strokes with a for loop
                  Beppo5 Level 1
                  kglad,
                  Thanks for all the help and attention here. The problem was I had strick typed the variable mc in my code but didn't use the var keyword. But still I can't get the code to work. The on events aren't being triggered for some reason.
                  I have distilled the code down to the for loop and a trace statement that executes onRollOver and onRollOut and nothing happens.

                  Just to go over what I have done. In the authoring enviroment I have created four movieclips named mcItemText0 ... 3.
                  I gave each an instance name mc_ItemText0 .. 3.
                  They all live on the same frame and the same layer.
                  On a sepprate layer but on the same frame I added my action script (attached below)

                  I am doing all of this in it's own file to get it right before I move it into my project (just to limit confusion as well as avoiding corruption of the file)

                  Maybe it will be easier to find what I am doing wrong with less going on in the code.

                  Thanks again.

                  ps. thanks for clearing up the 'this' mystery. It makes perfect sense to me know.

                  • 7. Trying to save key strokes with a for loop
                    Beppo5 Level 1
                    One more thing.
                    do I use the 'this' construction when refering to actionscript Objects created in the action script I am writting, or just for movieclips on the stage?

                    For example if I wanted to use a for loop to load strings from a text file would I write:

                    var this["objLoadVars"+i]:LoadVars;
                    this["objLoadVars"+i] = new LoadVars();

                    or is the 'this' not needed because the loadVars object doesn't sit in the timeline... or does it.
                    what about variables? would it be this["varName"+i]; or just ["varName"+i];

                    It has been these kinds of issues that I have been having for a long time. I finally feel comfortable enough understanding and writting code that I think I can figure it all out and start doing some cool stuff with flash.

                    Thanks