Results 1 to 8 of 8
  1. #1
    inbar7176 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default pressing two keys at the same time

    hi, im making a game in java which seems a little bit like super mario. i got a little problem with moving my character at an angle- so it can jump to the left/right. my code is going like this:

    public void keyPressed(KeyEvent evt) {
    int keycode = evt.getKeyCode();
    switch(keycode){
    case KeyEvent.VK_UP:direc2=3;direc=3;mario.direc=3;brea k;
    case KeyEvent.VK_LEFT: direc=2;mario.direc=2; break;
    case KeyEvent.VK_RIGHT:direc=1;mario.direc=1; break;}
    }
    direc is the condition of the character; when its 3 the character is jumping, when its 2 the character move to the left, and when its 1 the character moves to the right. the direc2 is in case when two keys are being pressed which are up and left/right. when i press the up key and another key at the same time the program consider only the up key and not the other one. so how can i change it?
    tnx for the help :)

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: pressing two keys at the same time

    Me personally, I don't do game logic directly in the event handlers because that is just concurrency madness. I rather let the event handlers update a given state (such as an array where the key event ID is the index to indicate that the key was pressed or not) and then have an overlapping game tick() method which is invoked at a steady pace using for example a Timer which will then do things and stuff for the game, taking the updated state of for example the mentioned key array into account.

    So in my example, the event handler would simply set the key state, and my tick() method invoked X times a second then comes along, which will in turn likely call an update() method on some sort of Player object, and that Player object will look at the key state array to see if the player needs to move this tick.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    inbar7176 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: pressing two keys at the same time

    Quote Originally Posted by gimbal2 View Post
    Me personally, I don't do game logic directly in the event handlers because that is just concurrency madness. I rather let the event handlers update a given state (such as an array where the key event ID is the index to indicate that the key was pressed or not) and then have an overlapping game tick() method which is invoked at a steady pace using for example a Timer which will then do things and stuff for the game, taking the updated state of for example the mentioned key array into account.

    So in my example, the event handler would simply set the key state, and my tick() method invoked X times a second then comes along, which will in turn likely call an update() method on some sort of Player object, and that Player object will look at the key state array to see if the player needs to move this tick.
    but i want to do my game with the way i did cause i dont really understand your way. the character is has to move only when keys are being pressed like in the super mario games.

  4. #4
    inbar7176 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: pressing two keys at the same time

    Quote Originally Posted by gimbal2 View Post
    Me personally, I don't do game logic directly in the event handlers because that is just concurrency madness. I rather let the event handlers update a given state (such as an array where the key event ID is the index to indicate that the key was pressed or not) and then have an overlapping game tick() method which is invoked at a steady pace using for example a Timer which will then do things and stuff for the game, taking the updated state of for example the mentioned key array into account.

    So in my example, the event handler would simply set the key state, and my tick() method invoked X times a second then comes along, which will in turn likely call an update() method on some sort of Player object, and that Player object will look at the key state array to see if the player needs to move this tick.
    i read your answer again and i understand what you're saying. i did too what you says, thats why i did the direc, which is the character's condition. but my question isn't connected to it at all. i just want to know how can i program the keypressed method so when two keys would be pressed at the sames time, the method would consider both of them. because now its consider only one key, even if two have been pressed. when i press the up and right keys the method "sees" only the the up key because its the first one which is processed. so how can i do it?
    Last edited by inbar7176; 12-12-2013 at 11:34 AM.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: pressing two keys at the same time

    If you press two keys, you get two key press events. Those two events will then flip both the key states in my array to 'YUP!' and then in the code I could check if both of them were pressed at the same time.

    Its not as easy as I describe it because you also have to deal with the fact that people can release a key too and they MAY just do that even before the tick() comes around; in other words if a player quickly taps a key you can miss that. I deal with that by actually recording separately if a key was pressed AND if it was released, using two separate booleans. Then I can react to the keypress and if the release boolean is also set, I can then mark the key as not pressed anymore at the end of my tick() method.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    inbar7176 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: pressing two keys at the same time

    Quote Originally Posted by gimbal2 View Post
    If you press two keys, you get two key press events. Those two events will then flip both the key states in my array to 'YUP!' and then in the code I could check if both of them were pressed at the same time.

    Its not as easy as I describe it because you also have to deal with the fact that people can release a key too and they MAY just do that even before the tick() comes around; in other words if a player quickly taps a key you can miss that. I deal with that by actually recording separately if a key was pressed AND if it was released, using two separate booleans. Then I can react to the keypress and if the release boolean is also set, I can then mark the key as not pressed anymore at the end of my tick() method.
    i changed it that way:

    public void keyPressed(KeyEvent evt) {
    int keycode = evt.getKeyCode();
    if(keycode==KeyEvent.VK_UP)
    {
    direc2=3;direc=3;mario.direc=3;
    }
    if(keycode==KeyEvent.VK_LEFT)
    {
    direc=2;mario.direc=2;
    }
    if(keycode==KeyEvent.VK_RIGHT)
    {
    direc=1;mario.direc=1;
    }
    }

    public void keyReleased(KeyEvent evt) {
    int keycode = evt.getKeyCode();

    if(keycode==KeyEvent.VK_LEFT)
    {
    direc=0;mario.direc=0;
    }
    if(keycode==KeyEvent.VK_RIGHT)
    {
    direc=0;mario.direc=0;
    }

    }

    but it still doent work

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: pressing two keys at the same time

    That looks like a mess, I have no clue what that is supposed to do. It also has very little to do with what I have been describing so far. I suggest you start over and start simple: only try to make moving to the right work, forget about any other direction for now. I would also advise you to use human-readable variable naming; 'direc' and 'direc2' are really not helpful.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    inbar7176 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: pressing two keys at the same time

    Quote Originally Posted by gimbal2 View Post
    That looks like a mess, I have no clue what that is supposed to do. It also has very little to do with what I have been describing so far. I suggest you start over and start simple: only try to make moving to the right work, forget about any other direction for now. I would also advise you to use human-readable variable naming; 'direc' and 'direc2' are really not helpful.
    i have also a run method which responsible for all movement, when some key is being pressed the direc value is being changed and the run method is leading to an update method which is updating the place and movement of the character, so the role of the direc is only to give the program the way the character has to go(left/right/up) and the direc2 role is to "tell" the program that the up key has beep pressed just in case another key would be pressed.
    moving to the right is working but when i try to mix it with moving up its not working
    Last edited by inbar7176; 12-12-2013 at 01:28 PM.

Similar Threads

  1. Interrupt thread by pressing Enter Key!
    By nadaa in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2013, 06:30 PM
  2. Replies: 0
    Last Post: 05-12-2013, 10:51 AM
  3. problem pressing buttons
    By mike28 in forum Java Applets
    Replies: 10
    Last Post: 10-30-2011, 03:19 AM
  4. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  5. Focusing the JTextField and so on while pressing Tab
    By britto_bicsjohn in forum AWT / Swing
    Replies: 1
    Last Post: 08-28-2009, 08:24 AM

Posting Permissions

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