5 Replies Latest reply on Feb 23, 2011 10:26 AM by Skoolbus777

    Click a keyboard key to make a mc play?

    Skoolbus777 Level 1

      I've been looking for this for years, and a good friend of mine that is a master at AS3 is not well versed in AS2, so I thought I'd ask the pros here.

       

      onClipEvent(mouseUp)

      on(release)

       

      I have code like these two examples on a mc (sometimes a button, I only use buttons in AS2) on the root timeline. Usually I use a mouse-click to activate them, but there's this one mc I'd like to have work via a keyboard keypress. What I can do is make the first frame of this mc blank, and the 2nd frame and beyond have elements to expose. So I was thinking I hit the key "v" to play this mc that resembles a volume overlay screen for this UI I'm working on.

       

      Greatly appreciate anyones help. I've googled this topic to death and have copy/pasted code with no luck.

       

      Thanks!

        • 1. Re: Click a keyboard key to make a mc play?
          Ned Murphy Adobe Community Professional & MVP

          Here's some sample code that you should hopefully be able to build from since you are familiar with AS3 at least.

           

          var keyListener:Object = new Object();
          Key.addListener(keyListener);

           

          keyListener.onKeyUp = function(){
              trace(Key.getCode()); // use this to determine key values while designing

           

              if(Key.getCode() == 86){

                  trace("clicked v");
              }


          }

          • 2. Re: Click a keyboard key to make a mc play?
            Skoolbus777 Level 1

            Hi Ned,

            Thanks for the help. Greatly appreciated.

             

            Variables are something that scare me! =) I've always dodged learning this because it's out of my league and grasp.

             

            With your supplied code, I'd like to incorporate a simple on(press) event. Where would I add code like this to your code you've provided?

             

            ------------------------------------------------

            on(release) {

             

            this.gotoAndPlay(_currentframe+0);

             

             

            }

            ------------------------------------------------

             

            Also this may seem like an incredibly stupid question... tracing? Why trace? I understand it helps you see where your code may be inoperable, but why trace a key value? Why not just find the ASCII code for the keypress? Is this trace within your supplied code just for convenience?

             

            Thanks again and I look forward to your response.

            • 3. Re: Click a keyboard key to make a mc play?
              Ned Murphy Adobe Community Professional & MVP

              That code cannot fit in with the code I provided because the code I provided goes in the timeline while the code you want to add gets attached to objects (poor practice).  To incorporate similar code in the timeline you would need to assign an instance name to the object you're pressing and use code like the following... (using objName as the name you assign for example)...

               

              objName.onRelease = function() {

                    objName.gotoAndPlay(_currentframe+0);

              }

               

              And using _currentframe+0 as an argument will literally get you nowhere.

               

              As far as why I added the trace, I was hoping the comment I added would make it clear why it's there... for convenience of finding the codes for different keys without having to look them up.  It would be commented out/removed in the final published file.

              • 4. Re: Click a keyboard key to make a mc play?
                Skoolbus777 Level 1

                Thanks Ned. I agree with you regarding tagging objects, and also regarding Scenes. But,,, I need to use both of these. What I do is mock up a UI the farthest I can take it... and after which it is given to a team of programmers. It's protocol as I'm just one guy and there are huge amount of programmers, since it is done in C+ in addition to using AS2. I use Scenes to help separate UI elements for the programmers, and I tag objects because it helps programmers (seeing quickly tagged objects) reprogram my mock into a better operational product. So I'm basically mocking in the simplest way using your basic onpress and onclipevent AS2s on objects (mcs, and buttons). I use buttons because it expedites the idea in a mock-up. I spend more time designing than I do programming. Again I totally agree. With scenes, when I'm passing on UI elements to programmers, I have to submit and separate my elements from the main mock in which is always an in-progress. So if I separate my UI into Scenes, I can keep in-progress of the whole snap, and individually deliver scenes based on deadlines. If I did everything on the root, and I had say 50 sections that deliver separately, I'd be in SOL. Again this is coming from a designer, so I claim no authority in AS because I have a lot to learn.

                 

                I use the currentframe+0 because I'm clicking an tagged MC on the root. once that mc hits it's stop, I have an option to click it again, and it plays until yet another stop. I wasn't sure if there was a simple "play" AS2 code, so I just used currentframe+0. It's working, but I'm sure there's a simpler way. I'm a designer with little programming experience. =<

                 

                I'm embarassed to say, that like most designers, I take code copy/paste it and see what happens. I regrettably don't understand the essence of the code, but I am in the process of learning.

                 

                So when I saw your code (below) I was hoping I could somehow make that call out an MC that I have on my root. That MC, when going into its nest, simply has stops. The tag on the mc (or if it's on the root timeline frame), I just want it to say: play this mc when I hit the letter "v". And that code I gave you is what I've been using, click to play the mc, the mc hits it stop, and click again to play the mc until the next stop. That _currentframe+0 is what I've been using to play. =/

                 

                var keyListener:Object = new Object();
                Key.addListener(keyListener);

                keyListener.onKeyUp = function(){
                    trace(Key.getCode()); // use this to determine key values while designing

                    if(Key.getCode() == 86){
                        trace("clicked v");
                    }

                 

                }

                • 5. Re: Click a keyboard key to make a mc play?
                  Skoolbus777 Level 1

                  So I found this by typing "on(" and up came a list of functions. I tagged this to my mc, and my mc's timeline has just stops.

                   

                   

                  on(keyPress "<Left>") {

                   

                  this.gotoAndPlay(_currentframe+0);

                   

                   

                  }

                   

                   

                  So I used <Left> and I utilize the left arrow key instead of "v". I tried putting in the <> the ascii code for "v". No luck. I'm happy with using the left, right, tab, enter, etc. options. Would be cool to define a key, but it serves its purpose.