• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

key update

 
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's going to fire if a listener is aded, but the code within keyPressed()
can be conditional

 
reply
    Bookmark Topic Watch Topic
  • New Topic