Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  KeyListener Why?  (Read 3001 times)
0 Members and 1 Guest are viewing this topic.
Offline trycoon

Senior Newbie




Java games rock!


« Posted 2005-05-09 00:33:05 »

I know this topic has been talked about over and over again, but as a newbee i don't yet get it.

Most example games i've seen use a KeyListener when detecting a pressed key and set a boolean variable like downArrow or k_button. Later some method do something like "if (downArrow) positionY += 1;"   The question is why can't we do this simple thing right when we detect the keypress?? Why all these booleans?? I can understand if there is a lot code for a keystroke that you want to move that block of code to another place making i cleaner. But for the example above it would only make it less readable.  Also if you got several Threads running i can understand it, but for single threaded apps?

What am i missing? PLEASE. =)
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2005-05-09 01:21:47 »

One reason is that you are supposed to do as little as possible inside a listener method.  These methods are called from the event dispatch thread.  The more time you spend in them, the longer other events have to wait.  Since all painting is called from this thread, you really don't want to hang it up.

Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2005-05-09 07:33:27 »

If you're using AWT there's no such thing as a single threaded app, since AWT runs a whole bunch of stuff in the background.

The size of the method isn't really the issue, since just adding one to a position really isn't any more hassle than setting a boolean.

The point is that you want the whole update process to be synchronised with your rendering. Since Events arrive on a seperate thread they can happen at any point during your rendering cycle. If for instance your main character was built up of say 5 sprites (maybe a tank with multiple guns?), you've drawn the first 3 then the event comes in. You update your players position straight away causing the next 2 sprites to be draw in a different position.

It only gets worse when you consider player position as a factor of scrolling where the whole screen can get out of sync half way through the rendering loop.

As always the ideal is to stick to a single thread, if you're using AWT to detect events the only way you can keep the truly single thread model is to control when your updates are performed based on the booleans.

Obviously there are other solutions to this, synchronising your rendering loop against event loops but this tends to lead to performance impact and unresponsive inputs.

The added benefit is that different platforms handle key repeat in different manners, using the booleans prevents this having any impact on your updates.

Hope this helps,

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2005-05-09 12:14:29 »

The thing is... if you press a key... an event is fired... ok.

If you release a key... an event is fired... ok.

But in between... well there can be something like no event for half a second and then some event hammering. How should your game handle that?

Well, of course... "not at all" is the easiest option, which means just using flags Wink

弾幕 ☆ @mahonnaiseblog
Offline trycoon

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-05-09 15:24:52 »

Thanks for all replies! I think i get the picture now..
Cheesy
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #5 - Posted 2005-05-09 17:15:43 »

Just to be sure, the KeyListener is only called once when you first press a key, and once when you release the key (disregarding key repeats for the moment because you don't want to rely on that in a game). Your game is probably running more frames in between the keypress and keyrelease events and you probably want your character to keep moving during that time. It's the difference between bashing your left button over and over again to move the player left, and pressing and holding the left button just once until you release it; or the difference between RSI after one game or after a thousend games  Wink

Offline gjbeyl

Junior Newbie




Java games rock!


« Reply #6 - Posted 2005-05-11 00:51:10 »

Quote
Just to be sure, the KeyListener is only called once when you first press a key, and once when you release the key (disregarding key repeats for the moment because you don't want to rely on that in a game). Your game is probably running more frames in between the keypress and keyrelease events and you probably want your character to keep moving during that time. It's the difference between bashing your left button over and over again to move the player left, and pressing and holding the left button just once until you release it; or the difference between RSI after one game or after a thousend games  Wink


Actually,  if you press and hold down a key, many key press events continue to fire.   That's why flagging is even more relevant, since for example someone may only intend only 1 movement space per press and release instead of every press.  There just isn't a 1:1 ratio of press:release.

Being a computer programmer, RSI is probably a forgone conclusion anyways.  Wink
Offline eraboin

Senior Newbie





« Reply #7 - Posted 2005-06-04 23:01:46 »

It's easier to use a Set object to store all the currently pressed keys, and then cycle through them during a game 'tick'. A set will store only one instance of each key, so you won't get duplicates... and that way it avoids creating lots of unnecessary booleans. You can use mySet.add(keyValue) on the keyPressed event, and mySet.remove(keyValue) on keyReleased. To cycle through them you can just get mySet.interator(), and make a simple while(iterator.hasNext()) loop. It's as simple as that.
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #8 - Posted 2005-07-12 10:10:30 »

keeping in mind that iterators will throw you an exception whenever the contents of the list they are iterating is changed, so you still have to be careful of asynchronous stuff going on here...
Offline SluX

Junior Member





« Reply #9 - Posted 2005-07-20 15:53:28 »

Umm...i agree with all above, but i think that u missed one important thing(or maybe i missed it cos my eyes are sleepy)-
This boolean array allows us to detect multiple key presses. If the code would just have in
public void keyPressed(Keyevent ke){
  if(somekey)do something
  if(otherkey) do something
...............

then our code would only detect last of them i think. But if we set a flag, then we can know if they are both pressed simultaniously.

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #10 - Posted 2005-07-20 15:58:03 »

Na, if you got multiple key presses you'd get multiple KeyEvents fired and hence keyPressed would be called multiple times allowing you to detect them.

Kev

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.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!