• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

key update

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a pong game. However when I moved the paddles with the key board I only get a smooth movement and the key had been down for say 0.5 seconds. Similar to the way in a word document if you hold a key, the program waits for a short time before a running sequence of letters get printed: eg

sssssssssssssssssssssssss

does anyone have any ideas.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use a timer to move the paddle

keyPressed starts the timer (also checks if timer is running, or boolean flag keyDown)
keyReleased stops the timer (resets keyDown to false)
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that advice Michael

I have another question.

If I want 2 players, is it possible to avoid the keyboard conflict when 2 or more buttons are pressed, or will I have to resort to using a mouse for 2 players
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to work OK for a couple of keys (in example left and right keys).
if you develop a conflict, try wrapping the actions in a SwingUtilities.invokeLater()

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the following code


is it possible to avoid triggering public void keyPressed in a certain condition so that the event would not be fired.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's going to fire if a listener is aded, but the code within keyPressed()
can be conditional

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic