Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 119,730 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,311 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Video Game Applet pt 2

 
Reply to this topicStart new topic

> Video Game Applet pt 2, Adding Async Keyboard input

Rating  2
WolfCoder
Group Icon



post 16 Jan, 2006 - 04:35 PM
Post #1


I was going to tech about MouseListeners for games but Asyncronized keyboard input is better for action games.

This is a rather short tutorial but it's valuable. Consider the following class definition:
CODE

import java.awt.event.*;
public class GameKey implements KeyListener // Keyboard input
{
    public boolean key_right,key_left,key_up,key_down,key_z,key_x;
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e)
    {
 if(e.getKeyCode() == e.VK_DOWN)
     key_down = true;
 if(e.getKeyCode() == e.VK_UP)
     key_up = true;
 if(e.getKeyCode() == e.VK_LEFT)
     key_left = true;
 if(e.getKeyCode() == e.VK_RIGHT)
     key_right = true;
 if(e.getKeyCode() == e.VK_Z)
     key_z = true;
 if(e.getKeyCode() == e.VK_X)
     key_x = true;
    }
    public void keyReleased(KeyEvent e)
    {
 if(e.getKeyCode() == e.VK_DOWN)
     key_down = false;
 if(e.getKeyCode() == e.VK_UP)
     key_up = false;
 if(e.getKeyCode() == e.VK_LEFT)
     key_left = false;
 if(e.getKeyCode() == e.VK_RIGHT)
     key_right = false;
 if(e.getKeyCode() == e.VK_Z)
     key_z = false;
 if(e.getKeyCode() == e.VK_X)
     key_x = false;
    }
}


It is a simple class that implements a KeyListener interface. It contains some booleans on the current state of the key. All of that key repeat rate stuff is bad for video games. Since the events are fired to key repeat rate specifications (There is a short pause until the event is fired continuously), this type of code is needed.

The boolean is set to true when THE VERY FIRST TIME the keyPressed event is fired.

To maintain the asyncronized input, the boolean is set to false immeadiatly when the key is released.

Now, here is a code cutout of how you would implement this into a panel or what have you:

CODE

setFocusable(true); // Required for keyboard input
GameKey game_key = new GameKey(); // Variable needed to get key states
addKeyListener(game_key); // Add it to your game


And when you want to read from the current state of the keyboard input, you simply do this:

CODE

if(game_key.key_z)
{
  // Key was pressed
}


If you want your own keys defined, add the corresponding code to keyPressed and keyReleased.

There you have it.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Fame Commeth
*



post 5 Mar, 2006 - 01:18 PM
Post #2
Wow! That solves so many problems! Thanks!

hehe:

postcount++;
Go to the top of the page
+Quote Post

jhoy
*



post 3 Feb, 2008 - 05:47 AM
Post #3
QUOTE(Fame Commeth @ 5 Mar, 2006 - 01:18 PM) *

Wow! That solves so many problems! Thanks!

hehe:

postcount++;

Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/15/08 04:36PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month