• 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

Weird game movement.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there, I've been making a couple basic games. I've made a basic swing application and a basic applet using AWT, and I've come up with a problem. The programs have a simple square that you move with the arrow keys. It seems that when I receive keyboard events there is a half second delay when a press down a key and when the square starts to move. I want to make it so that the square instantly starts moving in a direction instead of this weird delay. Can anyone explain to me what exactly is occurring here?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post a small sample program that demonstrates the problem
(just move the square in one direction)
 
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a wild guess. Maybe you should listen to the key pressed event instead of the key typed event.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alec,

I had the same problem if you mean the a....aaaaaaaaaaaaaaaaaaaaa phenomenon (happens every time you press a key in any common program in Windows). I've heard that this is an OS -feature and there's really nothing you can do about it. Correct me if I'm wrong.
I realised this when I wanted to make my sprites jump the first time. My character just froze in the air for a while and the rest of the jump went just like it was supposed to.
I fixed this by adding a thread (you can also use the java timer) in the key pressed-method that waits a certain equal time when any key is pressed. While running is not so obvious the jumping was really disturbing and I just made the velocity (how many pixels the sprite is updated left,right,up or down) a bit faster so the delay cannot be seen. I hope this was helpful.

Cheers,
Sarah
 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'm glad someone understands what I'm saying. I'm going to look into it further. Thanks.
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarah, I would suggest against solving it the way you did. Instead, you could for example use a boolean value movingUp, which you set when the up key is pressed, and which you unset when it is released. Then poll this boolean to determine whether the block should be moved. The same goes for the other keys.
reply
    Bookmark Topic Watch Topic
  • New Topic