开发者

KeyListener not listening when I change a boolean

Alright, i'm making a small pong game, and, i'm making a start menu for it. It's like,开发者_开发知识库 you start up the game, get a menu, you click on play, and then the game starts, now, everything works exactly as I wanted, except for one thing. When I click the button, everything gets drawn, the ball starts going, everything updates correctly, BUT, I can't move my players anymore. It's like the KeyListener has stopped working, my program suddenly stops reacting to keys. I'll give you guys all of my source code for the game, because, I am puzzled, I have no idea what's causing it, or how to fix it. My menu works by changing a boolean when you press the button, when the boolean is false, draw the start menu, and when it's true, draw the game. Right, since it's alot of code, I will upload it to pastebin.

PongWindow.java(includes main) http://pastebin.com/S5CpWW5Q

PongDrawing.java(includes drawing,moving,keylisteners,main game loop, and all of that) http://pastebin.com/x2LxkkbP

PongPlayer.java(the player) http://pastebin.com/N8VWTL0P

PongBall.java(the ball) http://pastebin.com/temGc2Yg

If you guys can figure out why it's not listening to my keys anymore, please tell, I have no idea.


It's like the KeyListener has stopped working,

Key events are only passed to components that have focus. If you click on a button then your game panel no longer has focus. You need to use something like:

gamePanel.requestFocusInWindow();

in your ActionListener code.

However the proper way to do this is to forget about using a KeyListener and instead use Key Bindings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜