A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: AS3 KEY_UP, KEY_DOWN Woes,

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3

    Unhappy AS3 KEY_UP, KEY_DOWN Woes,

    Hey guys, I have had a search for this, but please forgive me if there is already a post about it and I didnt see it, I really dont know what sort of query to use to find something about this.. x_x..

    Ok, here is my problem. Using the system illustrated in the book "Actionscript 3.0 Game Programming University" for keyboard movement (though it has a slight modification, with the additional keyCode parameters.) I have a problem which I just cannot seem to figure out. Here is the code for movement, which seems pretty standard for most CS3 keyboard control systems.

    Code:
    public var leftArrow,rightArrow,downArrow,upArrow:Boolean = false;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
    stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
    
    public function keyDownFunction(event:KeyboardEvent) {
      if (event.keyCode == 37 || event.keyCode == 65) {
        leftArrow = true;
      } else if (event.keyCode == 39 || event.keyCode == 68) {
        rightArrow = true;
      } else if (event.keyCode == 38 || event.keyCode == 87) {
        upArrow = true;
      } else if (event.keyCode == 40 || event.keyCode == 83) {
        downArrow = true;
      }
    }
    
    public function keyUpFunction(event:KeyboardEvent) {
      if (event.keyCode == 37 || event.keyCode == 65) {
        leftArrow = false;
      } else if (event.keyCode == 39 || event.keyCode == 68) {
        rightArrow = false;
      } else if (event.keyCode == 38 || event.keyCode == 87) {
        upArrow = false;
      } else if (event.keyCode == 40 || event.keyCode == 83) {
        downArrow = false;
      }
    }
    Ok, so that's the code I use, everything else, acts on if the boolean variables are set to true or false. The only modification to the code is the extra keyCode which enables the user to use UP,DOWN,LEFT,RIGHT And or, WASD too. However, that is not final, and the finished product will likely have the ability to designate your own keys... Anyway.

    My problem is this:

    The system is awesome, unless.. If WHILE HOLDING a key, the user right-clicks, or sometimes it happens on it's own, perhaps if the keyboard thinks you're pressing too many keys. ( It would usually happen too if the window lost focus, but I have a function in place to automatically pause the game and set all the movement variables to false in this case, so it does not matter. ) The game doesn't register that the user has released a key, and because of this, the variable remains true, while the user ISNT holding the key down. The only way to counter this is to have the window lose focus and it resets all of the variables to false, Or to have the user press the button corresponding to the True variable, and then release it.

    Is there a way I can prevent this from happening? I would really appreciate your assistance.

    Thanks for taking to time to read,
    Regards

    AEtharr-kun

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You may want to make use of this:

    http://www.uza.lt/blog/2007/08/solve...-click-in-as3/
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Quote Originally Posted by cancerinform View Post
    Yes, there IS that, which I have actually seen before.. however.. I do not wish to use it, as I would like to be able to use the game in either an online browser window OR a standalone executable.

    Not only that, but sometimes the problem has occurred of its own accord. Playing on my laptop for instance, It would sometimes do it, even if you didn't right click. Or if the window hadn't lost focus.. So, in those instances, the right click functionality wouldn't be of any use.

    Thank you for your suggestion though

  4. #4
    Member
    Join Date
    Aug 2007
    Posts
    47
    Hey there.
    If it's any consolation, I've been struggling with this too. I've likewise come to the conclusion that it's the arrow keys that are shifting focus. For me it only does it on the live game - I'm GUESSING focus switches away from the embedded swf (?) to some other element on the page. When testing the swf in Flash it doesn't seem to do it as (another guess) I presume the focus doesn't have anywhere to go.
    I've tried:
    a) Adding...
    tabChildren = false
    ...to the Document class, which is supposed to prevent any descendants from being part of the automatic tab order. And when that didn't work...
    b) Adding...
    this.stage.focus = this
    ...to the CurrentGame class at every game loop to refocus on the instance to which the eventListeners are added.
    Neither option worked once uploaded.
    ANY help well appreciated.
    If you want to see the problem (and game) in action, here's the link (You'll need to change the in game controls to keyboard to see the problem)...
    http://www.girlguiding.org.uk/browni...ponytales.html
    Occassionally (and sometimes regularly) the KEY_UPs fail to trigger for a few seconds and the horse keeps turning. Pretty frustrating, I imagine, for 7-10 year olds.
    Cheers.

  5. #5
    Member
    Join Date
    Sep 2010
    Posts
    37
    Okay I'm totally exhausted now from a whole night of video compositing, so I might be way off on this, but it looks to me as you could avoid the whole problem by changing your keyUpFunction() slightly.
    The way you have it now, if EITHER KEY inside the if statement is up, you're telling flash to put the appropriate "arrow" variable to false, so if you don't hold down BOTH AT THE SAME TIME, it will always try to set it to false... am i right?

    what you want is if BOTH the keys are up, to set it to false. so:
    Actionscript Code:
    if (event.keyCode == 37      ||      event.keyCode == 65) {
    should be:
    if (event.keyCode == 37      &&    event.keyCode == 65) {

    at least that's what made sense to me, at the moment.
    the keyDownFunction() should ofc stay the way it is.

    EDIT: Sorry, tired, the event can of course not be triggered by both keycodes at the same time... so what you want is more like
    Actionscript Code:
    if (event.keyCode == 37      ||      event.keyCode == 65) {
    if(leftArrow == true){ leftArrow = false; }

    okay im going into a coma now. gl hf kthxbye
    Last edited by Jumpkut; 10-21-2010 at 06:34 AM. Reason: Missed the point

  6. #6
    Member
    Join Date
    Aug 2007
    Posts
    47
    I've just noticed that I don't get this triggering delay problem in Firefox - at least it hasn't done it after about 5 minutes of play, when in IE it does it almost immediately.
    Last edited by moosefetcher; 10-21-2010 at 09:46 AM.

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thank you both for your responses, I will take a look at that idea when i get home later, I will report back if it helps or not.. Though It happens less often on a faster computer which is somewhat awkward..

    As a side note, the main problem is that Actionscript 3 does not report whether a Key is Currently pressed or up. The event triggers the moment the key has been pressed or released.. it isnt a check so much as a trigger.

    So if the focus has been lost While releasing the key, then the "KEY_UP" listener will not trigger, and the variables will remain the same.

    As i said, I will have a gander when I'm at home, thanks again.

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