Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  steering by the keys  (Read 1463 times)
0 Members and 2 Guests are viewing this topic.
Offline mannUndMeer

JGO n00b
*

Posts: 1


Java games rock!


« on: 2002-12-09 02:42:49 »

Hi Freaks,

I'm trying to build a little shoot-game just for fun. But I have problems to handle the keyEvents well.

My SpaceShip should fly to the direction as long as the key (for example: VK_UP && VK_RIGHT --> fly to north-east) is been pressed. by this way a lot of events are thrown and I don't know the right way to catch them (performantly).

Could you please post an example? To see, what I mean, please visit my applet:

http://www.gm.fh-koeln.de/~wi784/to/java/flight/flightJar.htm

The steering doesn't work valid - that's my problem.

greetings & thanks

Tobias
Offline cknoll

Full Member
**

Posts: 126


Flame On!


« Reply #1 on: 2002-12-09 06:04:19 »

Hi,
 The pattern I have seen used in games for capturing these sort of events is to just listen for the keypressed and keyreleased events and just set a boolean var to indicate which directional keys have been pressed.  In your movement loop, you don't wait for events, rather you just check the direction variables and process accordingly.   If a key is held down, I _think_ keytyped events are fired, but you don't want to handle those, you just want to handle keypressed and keyrelseased events.

-Chris
Offline lefty

JGO n00b
*

Posts: 16


Java ees kewl, man!


« Reply #2 on: 2002-12-09 17:38:43 »

Like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Game extends KeyAdapter
{
      private boolean up;
      private boolean down;
      private boolean left;
      private boolean right;

      private void mainLoop()
      {
            while (true)
            {
                  // check status of up, down, left, right
           }
      }

      public void keyPressed(KeyEvent e)
      {
            int code = e.getKeyCode();
            if (code = KeyEvent.VK_UP)
            {
                  up = true;
            }
            else if (code. = KeyEvent.VK_DOWN)
            {
                  down = true;
            }
            else if (code = KeyEvent.VK_LEFT)
            {
                  left = true;
            }
            else if (code = KeyEvent.VK_RIGHT)
            {
                  right = true;
            }
      }

      public void keyReleased(KeyEvent e)
      {
            int code = e.getKeyCode();
            if (code = (KeyEvent.VK_UP))
            {
                  up = false;
            }
            else if (code = KeyEvent.VK_DOWN)
            {
                  down = false;
            }
            else if (code = KeyEvent.VK_LEFT)
            {
                  left = false;
            }
            else if (code = KeyEvent.VK_RIGHT)
            {
                  right = false;
            }
      }
}
Games published by our own members! Go get 'em!
Offline lefty

JGO n00b
*

Posts: 16


Java ees kewl, man!


« Reply #3 on: 2002-12-09 17:41:01 »

Doh!.... except the if (code = KeyEvent.VK_XX) should be if (code == KeyEvent.VK_UP).
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.07 seconds with 19 queries.