0 Replies Latest reply on Feb 6, 2012 6:28 AM by Andrew Kinnear

    Key press elapsed time

    Andrew Kinnear Level 1

      I'm building a shooting game. I want to record how long the player has the F key pressed in order to use that value to remove ammunition sprites from the stage.

       

       

      My application loops in frame 1.

       

       

      I have a script that subtracts the key press time from the system time on key release. I think the repeat rate of the keyboard is interfering with this as the output figures are quite random and holding the key down for longer returns a lower number.

       

       

      global gFire, gTotal, gStartTime

       

      on startMovie

        the keyDownScript = "handleKeydown"

        the keyUpScript = "handleKeyUp"

      end startMovie

       

      --Handler to fire

      on handleKeydown

        if the key = "f" then

          gStartTime = the milliseconds

        end if 

      end handleKeydown

       

      --Handler to stop firing

      on handleKeyUp

        if the key = "f" then

          gfire = (the milliseconds - gStartTime)/10

          gLeft = gTotal - gFire

          gTotal = gLeft -- gTotal is used to incrementally turn off sprite visibility

          put gfire -- Trace elapsed time to Message window

        end if

      end handleKeyUp

       

      --This sequence removes bullets from the stage\

      according to he time spent firing

      on enterFrame

        if tTotal < 900 then

          sprite(10).visible = False --this repeated for a sequence of 10 sprites

        end if

      end

       

      The other problem with this is that it doesn't update the stage till the key is released, so a player can defeat the logic by just holding the key down continuously (especially because the value of gFire doesn't increase as expected)

       

       

      Ideally I'd like to record the elapsed time the key is down, add it to a global and have it update the stage at the frame rate, so I guess it would have to be within a frame event handler.

       

       

      Any help would be gratefully received.