Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Parallel Keyboard Inputs  (Read 1520 times)
0 Members and 1 Guest are viewing this topic.
Offline Wolfner

Senior Newbie





« Posted 2010-06-26 20:54:24 »

Hi there,

I'm working on my first Java game at the moment (which is a sidescroller like R-Type) and I have a little problem:

I added a KeyAdapter to my JFrame in order to move the spaceship on the screen.
Actually that works fine but with this approach I can only do one thing at the same time. So I can only move the ship OR shoot but i can't move the ship AND shoot at the same time.
I thought about parallelizing the input with threads somehow but I really have no clue how to do this.
Any ideas?


Cheers
Wolfner

P.S.:
This is how my KeyAdapter looks at the moment:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
private KeyAdapter keyAdapter1 = new KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent e) {
         if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            model.playerPositionChanged(1);
         } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            model.playerPositionChanged(2);
         }else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            model.playerPositionChanged(3);
         }else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            model.playerPositionChanged(4);
         }else if(e.getKeyCode() == KeyEvent.VK_SPACE){
            model.shot();
         }
      };
      public void keyReleased(java.awt.event.KeyEvent e) {
         model.playerPositionChanged(0);
      }
   };

Click to Play
Offline zoto

Senior Duke


Medals: 4



« Reply #1 - Posted 2010-06-26 21:53:34 »

A standard approach is to store all the key strokes in a boolean array using the integer returned from e.getKeyCode() as the index. A quick search of these forums came up with this post http://www.java-gaming.org/topics/super-easy-keyadapter-question/18715/view.html but I'm sure there is others.

EDIT*
Then in your update method of your ship you can check for each key you care about and handle it.
Offline Wolfner

Senior Newbie





« Reply #2 - Posted 2010-06-26 23:59:29 »

Oh, much simpler than i thought.
Thanks a lot!

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

Senior Newbie





« Reply #3 - Posted 2010-06-30 00:05:35 »

Hi again,

I implemented the controls and it worked out so far but I have one problem:
I can go in all 8 directions while firing (3-button combination) but one!
If I go "Up-left" the application just bleeps once and the ship doesn't move anywhere.

Cheers
Wolfner

Click to Play
Offline OverKill

Junior Duke




Java games rock!


« Reply #4 - Posted 2010-06-30 07:32:49 »

Yes, the boolean array is ok, but one thing you have to watch out for might be presses that are to quick to catch.
During my mobile dev days I did a similar approach and had this problem.
The problems happens when you store the key presses/releases and then in the main loop, poll the array. By the next iteration, they key once pressed might have been unpressed.

The alternative is to store presses and releases in different arrays. Then after each processing in the main loop, untoggle the pressed by checking if it had been released.
i.e.

1  
 if ( keyreleased[i] ) keypressed[i] = keyreleased[i] false;


This way you can even catch those quick presses.

Also, and this is why it was a problem with mobiles, some phones did not support repeats and some would not register a release if you were pressing two keys at once.

(posted this out of memory, I hope it is not failing me!)
Offline Wolfner

Senior Newbie





« Reply #5 - Posted 2010-06-30 11:03:24 »

Thanks a lot!
Just one further question:
Is there any way to override the delay between "KeyTyped" and "KeyPressed"?
(For example if you want to write "BBBBBBBBBBBBBBBBB", then there is a delay between the first B and the remaining Bs)

Click to Play
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-06-30 12:11:23 »

That delay is OS dependent (and configurable) but you shouldn't really be worrying about that.

Simply give your *weapon* a timestamp of when it was last fired, and give that weapon an interval between shots. Each gametick you check the weapon.canFire(), checking the whether currentTime > lastShotTime + weaponInterval, and if it returns true, call weapon.fire().

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline zege

Junior Newbie





« Reply #7 - Posted 2010-06-30 13:42:32 »

Timestamps might make the game act wierd, rather use a counter ;-)
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2010-06-30 19:47:01 »

Timestamps might make the game act wierd, rather use a counter ;-)

True, but only in a fixed rate engine update.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!