12 Replies Latest reply on Oct 7, 2009 7:48 AM by Ned Murphy

    multiple keyPress + tween scripts

    PlexJay

      I am doing a flashlite project (AS2) for an embedded application and am having trouble figuring something out. I want a single instance of a menu button that I can move with keyboard cursor presses to specific locations on the Y axis. Figuring out that part was easy but I am stumped on adding multiple commands to the same button.

       

      The initial script I am working with is:

       

       

      on (keyPress "<Down>") {

      import mx.transitions.Tween;

      import mx.transitions.easing.*;

      var xPosT:Tween = new Tween(high, "_y", regular.easeOut, -234.8, -186.8, 6, false);

      }

       

       

      Then I wanted to add something to the effect of:

       

      on (keyPress "<Down>") {

      if (y == "-186.8") {

      var xPosT:Tween = new Tween(high, "_y", regular.easeOut, -186.8, -135, 6, false);

      }

      }

       

       

      To tell it that if the button is at -186.8 go to -135.6. So obviously I am having trouble using keyPress "<Down>" twice. What would be the best way to approach this? I would like to have numerous keyPress "<Down>" and keyPress "<Up

        • 1. Re: multiple keyPress + tween scripts
          Ned Murphy Adobe Community Professional & MVP

          Assuming a couple things (which may be wrong to assume), the correct and a little more efficient way to do what you attempted could be...

           

          import mx.transitions.Tween;

          import mx.transitions.easing.*;

           

          on (keyPress "<Down>") {

              

               var finalY = 0; // some default value

           

               if (high._y == -186.8) {

                    finalY = -135.6;

               } else if(high._y == -135.6){

                    finalY = -95.6;

               } else... etc

           

               }

           

               var xPosT:Tween = new Tween(high, "_y", regular.easeOut, high._y, finalY, 6, false);

          }

          1 person found this helpful
          • 2. Re: multiple keyPress + tween scripts
            PlexJay Level 1

            Thank you so much for your response. Your suggestion looks fantastic. However I get the following compiler error when I try to use it

             

            LOCATION: frame=1, Line 1

            DESCRIPTION: ')' or ',' expected

            SRC: On (keypress "<Down>"){

            • 3. Re: multiple keyPress + tween scripts
              Ned Murphy Adobe Community Professional & MVP

              You'll need to show your code.  I'd say mine wuld work if it was complete, but I didn't exactly scrutinize it carefully when preparing it.  That message says you are using "On" rather than "on" which could be tripping things up.

              • 4. Re: multiple keyPress + tween scripts
                PlexJay Level 1

                You are right your code works fine. This is what I have:

                 

                on (keyPress "<Down>") {

                import mx.transitions.Tween;

                import mx.transitions.easing.*;   

                     var finalY = -186.8; // some default value

                 

                     if (high._y == -234.8) {

                          finalY = -186.8;

                     } else if(high._y == -186.8) {

                          finalY = -135;

                } else if(high.y == -135) {

                  finaly = -86.2;

                } else if(high.y == -86.2) {

                  finaly = -38;

                } else if(high.y == -38) {

                  finaly = 10;

                } else if(high.y == 10) {

                  finaly = 58.2;

                } else if(high.y == 58.2) {

                  finaly = 106.2;

                } else if(high.y == 106.2) {

                  finaly = 156.2;

                } else if(high.y == 156.2) {

                  finaly = 206.2;

                } else if(high.y == 206.2) {

                  finaly = 255.2;

                     }

                 

                     var xPosT:Tween = new Tween(high, "_y", regular.easeOut, high._y, finalY, 6, false);

                }

                 

                I had to put the import commands inside the on handler to make it work. The only problem I am having now is with the line:

                var finalY = -186.8; // some default value

                 

                In your example you had that set to 0. On the first keyPress <"Down"> the button went directly to 0 on the Y axis. I tried putting the value for the first stop on the Y axis, -186.8 and I am having trouble understanding whats happening. On the first keyPress <"Down"> the button goes to -186.8, the second goes to -135 but the third goes back up to -186.8. If I change the value to something higher like the final stop, 255.2 then on the first keyPress <"Down"> it goes directly to 255.2. I have attached the CS4 fla and swf for you to see.

                • 5. Re: multiple keyPress + tween scripts
                  Ned Murphy Adobe Community Professional & MVP

                  Do you really have "finaly" in the code?  It should be "finalY"

                   

                  I don't know what drives the values you use, but there is the possibility that you might not hit the values you are testing for exactly, so I don't know if you'd be safer having some margin for each conditional rather than a fixed value... something like...

                   

                  if(high._y > -236 && high._y < -234)

                  1 person found this helpful
                  • 6. Re: multiple keyPress + tween scripts
                    PlexJay Level 1

                    Unfortunately yes I am guilty of messy code I am not ashamed to say that I am a designer pretending to be a coder.

                     

                    I fixed the y but still don't understand what value I should put in

                    var finalY = 0;

                     

                    The y coordinates are taken from the design of the menu in the interface.

                     

                    Thanks again!

                    • 7. Re: multiple keyPress + tween scripts
                      Ned Murphy Adobe Community Professional & MVP

                      It's possible I'm misunderstanding something, so don't lose hope.  What problem are you currently having with it?

                       

                      If the _y value falls exactly on the values you use, then with the exception of having been moved to the last position, one of those conditionals should play out true, so the initial value of finalY should not be critical.  I'm just skeptical that it will actually land at exactly those values, which is why I'm suggesting to give yourself some wiggle room with the conditionals.

                      • 8. Re: multiple keyPress + tween scripts
                        PlexJay Level 1

                        Thanks Ned

                         

                        I have 11 positions on my menu that this button needs to tween to with keyPress <"Down"> and keyPress <"Up"> commands. The end user will only be using a keyboard or a remote control. Here is what it's doing:

                         

                        1. The button begins in position 1.

                        2. I press cursor down and the button tweens to position 2 which works correctly.

                        3. I press cursor down again and the button tweens to position 3 which works correctly as well.

                        4. The third time I press cursor down it takes it back up to position 2. Every subsequent cursor down push cycles it between position 3 and back to 2 and so on.

                         

                        Now if I change the value of var finalY = -186.8 to one of the other position values then the first cursor down goes directly to that position and subsequent pushes of cursor down does nothing. I hope that explains the situation better.

                         

                        Thank you!

                        • 9. Re: multiple keyPress + tween scripts
                          Ned Murphy Adobe Community Professional & MVP

                          Try putting a trace command in to check the value of high._y before your conditionals.  See if the value it is showing agrees with where it goes, as well as with the numbers you are using to judge it.

                           

                          on (keyPress "<Down>") {

                               trace(high._y);

                          etc....

                           

                           

                          I remain skeptical that the object moves to exactly the finalY values you specify... Flash is not an exact science

                          • 10. Re: multiple keyPress + tween scripts
                            PlexJay Level 1

                            Ok I did the trace. Here is the code I'm working with:

                             

                            on (keyPress "<Down>") {

                            trace(high._y);

                            import mx.transitions.Tween;

                            import mx.transitions.easing.*;   

                                 var finalY = -186.8 // some default value

                             

                                 if (high._y == -236.8) {

                                      finalY = -186.8;

                                 } else if(high._y == -186.8) {

                                      finalY = -135;

                            } else if(high._y == -135) {

                              finalY = -86.2;

                            } else if(high._y == -86.2) {

                              finalY = -38;

                            } else if(high._y == -38) {

                              finalY = 10;

                            } else if(high._y == 10) {

                              finalY = 58.2;

                            } else if(high._y == 58.2) {

                              finalY = 106.2;

                            } else if(high._y == 106.2) {

                              finalY = 156.2;

                            } else if(high._y == 156.2) {

                              finalY = 206.2;

                            } else if(high._y == 206.2) {

                              finalY = 255.2;

                                 }

                             

                                 var xPosT:Tween = new Tween(high, "_y", regular.easeOut, high._y, finalY, 6, false);

                            }

                             

                            (By the way, I do have it indented properly in the code but it pasted wierd for some reason)

                             

                            So when I test the movie here is the output I get on each keyPress <"Down">

                             

                            -236.75

                            -186.8

                            -135

                            -86.2

                            -37.95

                            -186.8

                            -135

                            -86.2

                            -37.95

                             

                            So I am not super concerned that it hits the right Y coordinate down to the decimal point. But as you see it starts rotating through coordinates once it gets to -37.95 instead of going to the subsequent coordinates represented in the code.

                             

                            Thanks!

                            • 11. Re: multiple keyPress + tween scripts
                              PlexJay Level 1

                              Sorry to bump this but I really need to figure this out. Thanks!

                              • 12. Re: multiple keyPress + tween scripts
                                Ned Murphy Adobe Community Professional & MVP

                                I already told you what is likely to happen and what you need to do to overcome it.  Your trace results show it happening.  Change your conditionals to allow for a range around the specific values you try to send things to.

                                1 person found this helpful