A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Keypress Question

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Post Keypress Question

    Is there a way to register only one key press?

    Currently i have a variable being added when you click the right key or being subtracted when you click the left key, and whilst it does work. Depending on how long you hold the key it will register add much more than the desired number.

    I can try and tap the key it still sometimes doesn’t work. Any help would be greatly appreciated. Thanks

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,818
    The keypress is an event. It'll keep firing when the event happens. I suggest that you use a boolean that you set to true on key down and false on key up. Right before setting it to true to check if it's false, if it is then change the variable.

    Side note. Why are you using AS2? Mainly just curious.
    .

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    16
    Thank you so much for this info! I’ll give it a try. I started working on this project a year and a half ago, and since it’s for Newgrounds and I was already planning on porting V2 to Unity with C#, I want to go ahead and finish V1 with As2, publish and then move to V2 on Unity. Figured it’s either starting all over with V1 on Unity or finishing this up with AS2 and starting V2 in Unity instead.

  4. #4
    Junior Member
    Join Date
    Jul 2018
    Posts
    16
    This is the closest code I got, someone on discord suggested it. Gonna put it here for when I go back to work on the project. I can find it quickly.

    function yourkeyUpFunction(){
    keypressed = false
    }

    Function enterframe(){
    if(Key.isDown(37) && !keypressed){//left key
    dowhatever()
    dosomestuff()

    keypressed = true
    }
    if(Key.isDown(39) && !keypressed){//right key
    dowhatever()
    dosomestuff()

    keypressed = true
    }
    }

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,818
    That's more than needed. You only want to run the code when the key is pressed. What you have there keeps checking multiple times a second. Try something like:

    Code:
    var keypressed = false;
    var key:Object = {
      onKeyDown:function() {
        if(!keypressed){
          keypressed=true;
          switch(Key.getCode()) {
            case Key.LEFT:
              trace('decrease var');
              break;
            case Key.RIGHT:
              trace('increase var');
              break;  
          }
        }
      },
      onKeyUp:function() {
        keypressed = false;
      }
    };
    Key.addListener(key);
    I did some looking around and this I feel is the best way to handle listeners in this case. I tested it so this works with as2.
    .

  6. #6
    Junior Member
    Join Date
    Jul 2018
    Posts
    16
    Quote Originally Posted by swak View Post
    That's more than needed. You only want to run the code when the key is pressed. What you have there keeps checking multiple times a second. Try something like:

    Code:
    var keypressed = false;
    var key:Object = {
      onKeyDown:function() {
        if(!keypressed){
          keypressed=true;
          switch(Key.getCode()) {
            case Key.LEFT:
              trace('decrease var');
              break;
            case Key.RIGHT:
              trace('increase var');
              break;  
          }
        }
      },
      onKeyUp:function() {
        keypressed = false;
      }
    };
    Key.addListener(key);
    I did some looking around and this I feel is the best way to handle listeners in this case. I tested it so this works with as2.
    Thank you so much!!! This worked perfectly 🙌🙌🙌🧡 you’re a life saver!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center