13 Replies Latest reply on Jul 16, 2008 2:01 PM by 4tonmantis

    Need Help!  Using Keyboard to Control MovieClip


      okay... I'm about knee deep in this project and I have come to a total stop. The title explains what I am attempting. I have run traces in just about every part of the code and it would appear that the input from the keyboard is simply not being read.

      There may be other problems with the code (I'm mostly self-taught) and I welcome ALL help... but the biggest worry is using the V key to turn the menu on and off... (I don't care if it's _alpha or visibilty... just as long as it's working). My current attempt is attached [note I didn't clean it from my last attempt so both of my attempts could be seen]:

        • 1. Re: Need Help!  Using Keyboard to Toggle Visibility
          4tonmantis Level 1
          Okay, I realized that this code needed to go into the instance of the movieclip with the interactivity. I don't care for that but at least I get feedback from my traces now.

          I have a new problem... the v key is being read constantly rather than as a single press. I understand why this is, but how do I get around this? I need it to be push V turn it on, push V turn it off... not hold down V to watch it blink and have an epileptic seizure...
          • 2. Re: Need Help!  Using Keyboard to Toggle Visibility
            Rothrock Level 5
            Do you need to listen for other keys while the v key is held down? I haven't done this before myself, so I'm only guessing here.

            But you could remove the listener on the key down and wait until there is a key up to restart the listener.

            Also I'm not sure what you mean by, "Okay, I realized that this code needed to go into the instance of the movieclip with the interactivity."

            Also I'm not sure how this is all supposed to be working. I don't see where hideShowMenu is being called. And I'm not sure in your onKeyDown event handler why you have just a "showMenu" there?

            • 3. Need Help!  Using Keyboard to Toggle Visibility
              4tonmantis Level 1
              Thanks for replying. I didn't update my code here after fixing it. Inside the init function that should be a call to hideShowMenu...
              I commented out the listener and the interactivity works without it...in fact the listener was not returning a trace at any point in time. I moved the code to the menu item's movie clip. I also changed it to tween to an alpha fade so that a gotoAndPlay would suffice rather than controlling visibility and the like.

              What currently happens:
              User: presses V key

              Menu: transitions to Open or Close (the boolean is working, as the transition is correct and a good trace for each is returned).

              However: if the user holds the V key down for longer than a quick tap, the menu freaks out, cycling quickly between the transitions. Clearly, this is not ideal, as the idea is for a simple "hide" "show" toggle.

              EDIT: Oh... I didn't talk about other keypresses. Once I have this code working, I should be able to reasonably deduce the other keyboard interactivity. The V Key is only supposed to be pushed, not held. However, programming a failsafe might not be a bad idea.
              • 4. Re: Need Help!  Using Keyboard to Toggle Visibility
                Rothrock Level 5
                I'm pretty sure I understand what you are trying to do. I even think I might see where some things are going wrong. But I really don't know what you are doing. It would be very helpful if you shared your code.

                As for the alpha tween and all you've added a bunch more ways that thing can go wrong.

                There is no reason that I know of why the code would need to be in the clip itself and personally I don't like to do that because it makes it hard to debug and modify later.

                Also visibility is much easier to work with than alpha, so I'm confused why you mention "controlling visibility and the like." in a way that makes it seem like it is the difficult path? Or is that just me reading too much into it. The problem with _alpha is that if you have interactive buttons or such in the _alpha=0 movie, they are still active and accessible. So it is almost always a good idea to set _visible=false once you get _alpha=0. Tricky!

                I'll give this a try later today and see if I can come up with anything!
                • 5. Re: Need Help!  Using Keyboard to Toggle Visibility
                  Rothrock Level 5
                  Okay. Here is the basic thing I came up with. A clip on the stage called myMenu and this code goes on the frame of the main timeline. Don't forget that if you are trying this in the testing environment that you need to go up to the control menu and disable keyboard shortcuts.

                  If you want to use tweens (and that is great) then you also need to add something that checks if it is tweening already and stops it or rewinds it or something.
                  • 6. Need Help!  Using Keyboard to Toggle Visibility
                    4tonmantis Level 1
                    I haven't looked at the code you posted yet, but I will as soon as I've finished posting this.
                    First...Thank you so much for your time and attention on this.
                    Now... I'm attaching my code. It has been commented so you can see where I was going with it. Timeline is just a stop and variable declarations.
                    The other code was put into the movieclip instance on the stage as an experiment because I wasn't getting ANY response to keyboard input and so far it works.
                    My movieclip instance contains a keyfamed transition (I've used tween classes before but I'm doing 3d, video, and flash on this project so shortcuts are unfortunately required for time). The references in the hideShow are calling the sequence that contains the keyframed transitions, which includes scaling, moving, fading, and rotating to summarize.

                    Anyway... Here's the code... I'll be looking at and trying what you have provided... if you can look at what I've got... it might be salvageable... in the meantime I'll be mucking around.
                    BTW: How do you disable the hotkeys? That has been driving me bonkers... EDIT: I forgot to mention I am using Flash CS3
                    • 7. Re: Need Help!  Using Keyboard to Toggle Visibility
                      4tonmantis Level 1

                      Rothrock you're a freakin genius!!!
                      I added the gotoAndPlay to the first part so when it becomes visible, it also plays the transition... the only thing I can't figure out is how to do the opposite (play the closing transition). I'll be looking into it but THANK YOU!!! If you're ever in Virginia Beach, I owe you a beer!
                      • 8. Re: Need Help!  Using Keyboard to Toggle Visibility
                        Rothrock Level 5
                        Ah I see some of what you've got going on here. Part of the problem with your old code was that you had two testers going all the time -- as it were.

                        One way to test if a key is down is to use an onEnterFrame and every frame ask "Hey, is this key down, hey is this key down, hey is this key down, etc..." That uses a lot of resources and is like a little kid in the backseat saying are we there yet all the time. It works, but it isn't so elegant!

                        The other way is to listen for the key and when the key says, "Hey I'm down." To then ask, "Okay, which key is down?" That is what a listener does. It is more elegant and is really oriented toward the event based model of what AS has become in AS3. (So if you plan on continuing with Flash and Actionscript listeners are a good thing to get a handle on!)

                        Ultimately onEnterFrame is an event as well, it is just an event that happens a lot more often than you need for what you are trying to accomplish. I don't know anyone who can type at even 12 characters a second.

                        It can get tricky with the animation, because you actually don't just want to switch the fade out (or whatever) directly to _visible=false. So you should probably go back to using a variable like your showMenu. I would prefer a name like isShowing because showMenu sounds more like a method than a property and when you come back to this is six months it might be confusing.

                        For the fade out part, I would probably just make sure that the last frame of the clip was empty and that way you wouldn't have to worry about the _visible part. There are all kinds of ways to do that part. So again I'm not sure exactly what to tell you.

                        • 9. Re: Need Help!  Using Keyboard to Toggle Visibility
                          4tonmantis Level 1
                          The menu doesn't have buttons in it... is it possible to get this sort of functionality minus the calls to visiblity? The animation includes keyframed fading already so the main emphasis is getting the animation/transitions to play completely.

                          I guess this is no longer "Need Help! Using Keyboard to Toggle Visibility" and more... "Need Help! Using Keyboard to Play Movieclip".

                          PS. I see what you're saying about the naming convention... that makes sense and I"ll have to keep it in mind. Thanks a million man.
                          • 10. Re: Need Help!  Using Keyboard to Toggle Visibility
                            4tonmantis Level 1
                            Wait a minute... couldn't I just run my function from an onKeyUp handler rather than a complex expression for the key being down?

                            I didn't know about onKeyUp or I would've asked about it sooner but simply testing the condition when the key is released makes much more sense... Just do a condition check of the boolean in the onKeyUp and if it's true gotoAndPlay(2); else it would gotoAndPlay(16);

                            Would that work?
                            • 11. Re: Need Help!  Using Keyboard to Control MovieClip
                              Rothrock Level 5
                              You could use the onKeyUp if you want code to run when they let go of the key. If that is how you want it to work there is no reason it shouldn't work that way!

                              As for not knowing about onKeyUp, here is a trick for learning about Flash. Everything in Actionscript is organized by classes. Now knowing what class you need can be a bit tricky to begin with, but once you've got your first bit -- in this case either Key.isDown() or onKeyDown you are good to go. Those are a method and an event of the Key class. So then you can go into the help files and look for the Actionscript dictionary -- don't do a search you'll never find what you need.

                              In the AS 2 Dictionary and Language Reference all the classes are in alphabetic order. So then you can just scroll down to the Key section expand the folder for Key and there is everything that you can do with the Key class.

                              When ever I start working with a new class or one I haven't used a lot I always go there and review what is available to me.

                              As for the naming convention it doesn't matter if you use mine or whatever, but definately get some conventions and then use them. It makes life so much easier. This also ties in with help files I mentioned earlier. There are pretty much three types of entries there: Properties, Methods, and Events.

                              The properties are just that, characteristics. They are the things you are familiar with like _alpha, _visible, _width, etc. They are generally nouns. I generally like to give my properties names of nouns. But for Booleans I like to use the "is" or "has" prefix to mark it as a yes or no type property.

                              Then in the help files you will see methods. They are generally name with a verb or a verb-noun combination. They will also have parenthesis after their name to indicate they are a method. Some popular methods you might know are gotoAndPlay() or getKey().Some methods require arguments to go in the parens others don't. If you make functions in your code those are similar to methods and should be named like them.

                              Finally there are events. They are generally names with "on" and then some verb or verb-noun combination. Popular events might be onRelease or onEnterFrame. So these are the things that happen and you can write code in response to them. Generally you won't be naming events until you start writing your own classes, but when you see the name you will know what it is.

                              Also remember in AS2 there are three ways that you can handle events, so always check the help files. Some you just assign a function, like myClip.onRelease, others like the Key class you Key.addListener() (verb+noun and parens -- addListner must be a method!), and finally others, like the MovieClipLoader class, you use addEventListener().

                              These are just some general guidelines, but they are pretty useful to know.
                              • 12. Need Help!  Using Keyboard to Control MovieClip
                                4tonmantis Level 1
                                That's great info. I've been mucking around in AS2.0 for maybe a year or 2 off and on and those are very good guidelines.

                                I have tried to adapt the listener code and the KeyUp function but it's just not working. I had one version where when you pushed down it would unfold the menu (the open transition) and when you let up, it would close... which is kinda close but still a ways off... and buggy... I moved forward and made a few more changes and can't get it to behave... here's what I have if you have time to peek...
                                • 13. Need Help!  Using Keyboard to Control MovieClip
                                  4tonmantis Level 1
                                  EUREKA! <dorkiest moment in a loooong while....

                                  I figured out how to get what I want... here's the code... please let me know if there's a more elegant way of doing it or if my comments indicate that I am clueless about what I have done... thanks!

                                  EDIT: I accidentally discovered that I had left out a condition for the else statement, forcing the else into an exclusive relationship... went in and added an if statement to the else condition and viola!

                                  Thanks Rothrock for all your help... without you I wouldn't have gotten this finished!