Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Key Presses only work randomly  (Read 569 times)
0 Members and 1 Guest are viewing this topic.
Offline Rakso

Junior Member


Medals: 1



« Posted 2013-05-03 20:30:00 »

This is such a strange problem..

Basically i have created a key listener connected to my JPanel.. And sometimes when i compile and run it it does work realy good and no problems what so ever..
But then sometimes when i compile it wont work. It doesn't recognise keypresses.

Here is the code i used for key listener in my JPanels update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
        if(keyListener.getMovingRight() == KeyEvent.VK_RIGHT){
            player.playerMoveRight();
        }
        if(keyListener.getMovingLeft() == KeyEvent.VK_LEFT){
            player.playerMoveLeft();
        }
        if(keyListener.getMovingUp() == KeyEvent.VK_UP){
            player.playerMoveUp();
        }
        if(keyListener.getMovingDown() == KeyEvent.VK_DOWN){
           player.playerMoveDown();
        }


And the above code knows if keys was pressed from this class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
public class KeyBoard implements KeyListener {

    private int playerMoveUp;
    private int playerMoveDown;
    private int playerMoveLeft;
    private int playerMoveRight;

    public KeyBoard(){
        playerMoveUp = 0;
        playerMoveDown = 0;
        playerMoveLeft = 0;
        playerMoveRight = 0;
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {
    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {

        //Switch statement to get which keys were pressed
       switch(keyEvent.getKeyCode()){

            case KeyEvent.VK_UP:
                playerMoveUp = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_DOWN:
                playerMoveDown = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_LEFT:
                playerMoveLeft = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_RIGHT:
                playerMoveRight = keyEvent.getKeyCode();
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {

        //Switch statement to get which keys were released
       switch(keyEvent.getKeyCode()){

            case KeyEvent.VK_UP:
                playerMoveUp = 0;
                break;

            case KeyEvent.VK_DOWN:
                playerMoveDown = 0;
                break;

            case KeyEvent.VK_LEFT:
                playerMoveLeft = 0;
                break;

            case KeyEvent.VK_RIGHT:
                playerMoveRight = 0;
                break;
        }
    }

    public int getMovingUp(){
        return playerMoveUp;
    }
    public int getMovingDown(){
        return playerMoveDown;
    }
    public int getMovingLeft(){
        return playerMoveLeft;
    }
    public int getMovingRight(){
        return playerMoveRight;
    }
}


And i ofcourse added the key listener to the JPanel by doing this:
1  
panel.addKeyListener(KeyBoardClasshere);



Since it wasnt working regularly i tried something else called Key Bindigs since i heard that would increase my chances..

I added this in my JPanel class and commented out the key listener in the update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
    public void keyBindingsInitialize(ActionMap am, InputMap im){
       im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Right");
      am.put("Right", RightBind);
     
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Left");
      am.put("Left", LeftBind);
     
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Up");
      am.put("Up", UpBind);
     
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Down");
      am.put("Down", DownBind);
    }
   
    Action RightBind = new AbstractAction(){
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        player.playerMoveRight();
      }
    };
    Action LeftBind = new AbstractAction(){
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        player.playerMoveLeft();
      }
    };
    Action UpBind = new AbstractAction(){
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        player.playerMoveUp();
      }
    };
    Action DownBind = new AbstractAction(){
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        player.playerMoveDown();
      }
    };


This also works but sometimes when i compile and run the project it just doesnt work and then it works if i terminate the project and then run it again.. Its soo strange in my oppinion.. Anyone have suggestions?
Offline Rakso

Junior Member


Medals: 1



« Reply #1 - Posted 2013-05-03 21:27:35 »

Solved it by adding frame.revalidate(); in my game loop. Thanks for all answears!
Offline matheus23

JGO Kernel


Medals: 108
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2013-05-04 09:29:56 »

Thanks for all answears!

I guess you were ironic? Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rakso

Junior Member


Medals: 1



« Reply #3 - Posted 2013-05-04 11:19:38 »


Oh not at all im very sorry, I posted the question also in StackOverflow cause sometimes problems this comunity cannot solve they can. And after i found the solution myself i just posted than on stack overflow and then pasted it here.
Sorry i did not mean to look down on this comunity in any way.
Offline matheus23

JGO Kernel


Medals: 108
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2013-05-04 11:23:46 »


Oh not at all im very sorry, I posted the question also in StackOverflow cause sometimes problems this comunity cannot solve they can. And after i found the solution myself i just posted than on stack overflow and then pasted it here.
Sorry i did not mean to look down on this comunity in any way.

Don't get me wrong, I was kidding Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Rakso

Junior Member


Medals: 1



« Reply #5 - Posted 2013-05-04 11:24:56 »


Oh not at all im very sorry, I posted the question also in StackOverflow cause sometimes problems this comunity cannot solve they can. And after i found the solution myself i just posted than on stack overflow and then pasted it here.
Sorry i did not mean to look down on this comunity in any way.

Don't get me wrong, I was kidding Cheesy
Yeah im not Smiley, Just wanted to make sure you did not get me wrong Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!