3 Replies Latest reply on May 16, 2007 4:02 PM by Newsgroup_User

    Keypress question

    imiccidesign
      Hi
      I am completly new to action scriping and have tried diligently to find the answer in the forums but most of it such a foreign language to me, so I am hoping someone can help me out with a straight answer and the exact bit of code I need.

      What I need flash to do:
      when i press the key "J" it jumps to frame 56 and plays a movie clip named "j2".
      when I let go, the flash file continues where it left off.

      I am using Flash 8

      Thanks very much
      Bill
        • 1. Re: Keypress question
          Level 7
          Bill,

          > What I need flash to do:
          > when i press the key "J" it jumps to frame 56 and plays a
          > movie clip named "j2".
          > when I let go, the flash file continues where it left off.

          To start, I'll need to know what version of ActionScript you're
          publishing to. By default, the recently released Flash CS3 publishes
          ActionScript 3.0. Flash 8 publishes to ActionScript 2.0.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Keypress question
            imiccidesign Level 1
            I am using Flash 8
            Thanks
            • 3. Re: Keypress question
              Level 7
              Bill,

              >> when i press the key "J" [should] jump to frame 56 and play a
              >> movie clip named "j2".
              >> when I let go, the flash file continues where it left off.

              > I am using Flash 8

              Okay, the basic "template" for responding to key strokes in ActionScript
              2.0 is this:

              var listener:Object = new Object();
              listener.onKeyUp = function():Void {
              trace("A key has been pressed");
              }
              Key.addListener(listener);

              In line 1, an arbitrarily named variable, listener, is declared and set
              to an instance of the Object class. This gives you a generic object that
              you can use for just about everything. (Ultimately, everything in
              ActionScript *is* an object -- and each type of Object is defined by its
              class entry in the ActionScript 2.0 Language Reference. Look up "MovieClip
              class" to see what features are available to movie clips; look up "TextField
              class" to see what features are available to dynamic and input text fields,
              and so on.)

              In lines 2 - 4, an onKeyUp property is created for the listener object.
              Why onKeyUp? That's an event of the Key class, which handles
              keyboard-related phenomena. In this case, you're building an "event
              listener," and the listener object acts as a liaison for the events of the
              Key class. An anonymous function literal is assigned to this onKeyUp event,
              and the function simply traces a message to the Output panel.

              In line 5, the Key class is associated with the listener object, which
              wires everything up.

              In your original post, you spoke of sending the playhead somewhere when
              a key is pressed, rather than released. That's fine ... consult the "Key
              class" entry in the documentation and you'll find that an onKeyDown event
              also exists. No only that, but a number of properties, methods, and events.
              To do something when only the "J" key is pressed, you could use an if()
              statement ...

              var listener:Object = new Object();
              listener.onKeyDown = function():Void {
              if (Key.isDown(74)) {
              // do something
              }
              }
              Key.addListener(listener);

              What on earth is Key.isDown()? That's a method of the Key class.
              What's 74? That's the key code for the letter "J" (upper- or lowercase).
              How did I know that? I searched "Keyboard Keys and Key Code Values" in the
              Help docs.

              In place of "// do something," you would, of course, program what you
              actually want to occur. Based on your original post, that would be
              something like ...

              var listener:Object = new Object();
              listener.onKeyDown = function():Void {
              if (Key.isDown(74)) {
              gotoAndStop(56);
              }
              }
              Key.addListener(listener);

              ... that's a start, at least. If you also want a movie clip on that frame
              to play, you'll have to reference that movie clip by its instance name and
              invoke the MovieClip.play() method ...

              var listener:Object = new Object();
              listener.onKeyDown = function():Void {
              if (Key.isDown(74)) {
              gotoAndStop(56);
              j2.play();
              }
              }
              Key.addListener(listener);

              IMPORTANT: If the j2 MovieClip instance doesn't already appear in
              whatever frame carries this code, you won't be able to reference j2 --
              because "it doesn't exist" on that frame. You'll have to either stretch out
              that movie clip in the timeline so that it does, and maybe set its
              visibility to 0, then do something like this:

              j2._visible = false;
              var listener:Object = new Object();
              listener.onKeyDown = function():Void {
              if (Key.isDown(74)) {
              gotoAndStop(56);
              j2._visible = true;
              j2.play();
              }
              }
              Key.addListener(listener);

              At any rate, I hope that gives you a good start.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."