7 Replies Latest reply on Mar 29, 2011 5:28 PM by kglad

    Holding Down a Key: Execute ONCE

    EranthisAconite

      I'm very lost on my scripting for holding a button down and having the action take place once.

       

      So thing is, I have code for pressing a button (which is the up arrow button)

      This was suppose to show the animation for someone shooting constantly until released:

       

          if (Key.isDown(Key.UP)){
              fox.gotoAndPlay(16);
              deploy.gotoAndPlay(2);
              trace("Pressed.");
          }

       

      Unfortunetly the results are:

      Holding down the up key places out "Pressed." constantly instead of once.

      fox.gotoAndPlay(16) stays on 16 rather progressing the animation (to which should come back to 16 at a repeated process)

      deploy.gotoAndPlay(2) plays a sound but before: the sound played constanlty rather once) recently: sound never plays

       

      I also need code that prevents the player from moving when holding down the up arrow key.

      So in total, I want the player to be able to hold the up key, and everything executed is played once until pressed agan after release.


      I heard about adding listeners:

       

           var myListener:Object = new Object();
      myListener.onKeyDown = function() {
          KEY = Key.getCode();
          if (!this[KEY]) {
              trace("You pressed "+KEY);
          }
          this[KEY] = true;
      };
      myListener.onKeyUp = function() {
          KEY = Key.getCode();
          trace("You released a key."+KEY);
          this[KEY] = false;
      };
      Key.addListener(myListener);

       

      but I can't understand this technobabble and everytime I try to work it, it only puts error outputs.

      Please help. This needs to be finshed in under 4 days.