Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (563)
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  
  KeyListeners...  (Read 2243 times)
0 Members and 1 Guest are viewing this topic.
Offline Shin_Bet

Senior Newbie





« Posted 2006-01-19 23:11:03 »

Hi, im having problems with my keylisteners. Theyre threaded, and the problem is that when i switch the key, it takes a while fo the computer to realize it. The effect is that, for animations, i get one picture moving across the screen until it changes, or when i switch keys quickly the image freezes for a bit. Any help is appreciated. Huh Huh Huh
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #1 - Posted 2006-01-20 03:51:37 »

You use Threads for listening!?

That's not how you do it. Check the swing tutorial.
Offline Grand Poeba

Junior Member





« Reply #2 - Posted 2006-01-20 14:21:16 »

you can use that to check if multiple keys are pressed at once so it is ok
with the normal keylisterne you cant see if key UP and RIGHT are preseed together
because it only registers one event
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2006-01-20 15:05:39 »

if you keep an array of key states, you can check if two keys are pressed at the same time without using threads. I wrote an example of this recently on the Sun forums, check it out: http://www.woogley.net/misc/MultiKey/

now that you have this info, stop threading keylisteners Wink
Offline Shin_Bet

Senior Newbie





« Reply #4 - Posted 2006-01-20 21:13:49 »

It works even worse if i dont use threads! When i hold down a key, it registers it once, then pause, THEN goes on quicker. How do i remove that inital pause?
Offline woogley
« Reply #5 - Posted 2006-01-20 22:12:27 »

that pause is from the OS itself (in windows it's "repeat delay"). if you use the example I gave you and check key states OUTSIDE of the event loop, you won't have to worry about pauses.
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #6 - Posted 2006-01-21 01:02:10 »

When a key is pressed, set a corresponding boolean to true. When it is released, set that boolean to false. Poll your booleans once every frame. Don't ever mess with Threads until you understand stuff like public synchronized void doSomething() or blocks like synchronized(lock){ doSomething(); }. I will be so bold as to assume you're not entirely familiar with those expressions. Please correct me if this assumption is wrong. There's a reason why games normally execute most or all code from a single Thread, and it's called synchronization.
Offline pin_kushn

Senior Newbie




Java games rock!


« Reply #7 - Posted 2006-01-21 02:27:30 »

Woogley:

Thanks for posting your MultiKey example.  Is there an advantage to listening that way instead of creating and adding KeyListeners?

Offline woogley
« Reply #8 - Posted 2006-01-21 02:55:37 »

Woogley:

Thanks for posting your MultiKey example.  Is there an advantage to listening that way instead of creating and adding KeyListeners?

well, if you're programming for the 4K contest, it'll save you some bytes Wink

the only other advantage is you don't have to worry about defining each method for the listener even when you dont use them (e.g. you only want keyTyped but you have to define keyPressed and keyReleased to compile).
Offline Mr_Light

Senior Member




shiny.


« Reply #9 - Posted 2006-01-23 16:08:33 »

When a key is pressed, set a corresponding boolean to true. When it is released, set that boolean to false. Poll your booleans once every frame. Don't ever mess with Threads until you understand stuff like public synchronized void doSomething() or blocks like synchronized(lock){ doSomething(); }. I will be so bold as to assume you're not entirely familiar with those expressions. Please correct me if this assumption is wrong. There's a reason why games normally execute most or all code from a single Thread, and it's called synchronization.
I always thought it was because developers don't know better and fear threads as the plage. Grin

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2006-01-23 22:23:51 »

<project plug>

'course you could sidestep ALL this event crap if you use JInput.

</project plug>

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Grand Poeba

Junior Member





« Reply #11 - Posted 2006-01-23 23:07:34 »

<project plug>

'course you could sidestep ALL this event crap if you use JInput.

</project plug>

find it rather uneasy to use jinput :$
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #12 - Posted 2006-01-24 00:07:17 »

I haven't looked at JInput for a while, but I can understand why you say that.  At the time I was trying to help out with the Mac port and I never did get my head wrapped around how the heck it was supposed to work Smiley .. of course at the time there is a good chance that was because it didn't Smiley

Is there good documentation of JInput yet?

I'm hoping that soon some general purpose USB bindings make it into the core JRE and then things like JInput can be done in pure Java.  (Kind of like Java COMM should have been.  Hello, Sun? Remember that concept of client-side Java?  It doesn't work so well when you can't interface with the client Smiley.)

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.

Dwinin (16 views)
2014-09-12 09:08:26

Norakomi (50 views)
2014-09-10 13:57:51

TehJavaDev (58 views)
2014-09-10 06:39:09

Tekkerue (27 views)
2014-09-09 02:24:56

mitcheeb (50 views)
2014-09-08 06:06:29

BurntPizza (34 views)
2014-09-07 01:13:42

Longarmx (20 views)
2014-09-07 01:12:14

Longarmx (23 views)
2014-09-07 01:11:22

Longarmx (22 views)
2014-09-07 01:10:19

mitcheeb (31 views)
2014-09-04 23:08:59
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!