Movement with the keyboard

I am trying to write a game that uses the keyboard arrow keys for movement. By pressing and holding an arrow key, the object should move accordingly, however there is a short pause before the program recognises that the key is being held down, is there anyway to remove this delay?
[288 byte] By [IIIIIIlla] at [2007-9-28 14:11:20]
# 1
I've seen keyboard games (including mine) that respond quick enough for players not to notice any delay. So if you post some code, we can help you.Kind regards, Levi
levi_ha at 2007-7-12 10:33:59 > top of Java-index,Other Topics,Java Game Development...
# 2
Are you using J3D or Swing/AWT?
Breakfasta at 2007-7-12 10:33:59 > top of Java-index,Other Topics,Java Game Development...
# 3

this is not a java problem, what you need to do is decrease your computers repeat delay. you do that by going to the control panel and double-clicking on keyboard. then on one of the tabs will be a slider called Repeat delay. drag that all the way down as close to short as you can. hope that helps

acethrowbota at 2007-7-12 10:33:59 > top of Java-index,Other Topics,Java Game Development...
# 4

Well, what I do is make a while loop, when the button is pressed

it will go through the loop, and do whatever that button is

suppose to do.

void up_Pressed(){

while(button_pressed){

//do animation or whatever

}

}

public void keyPressed(KeyEvent e){

if(e.getKeyCode() == KeyEvent.VK_UP){

up_Pressed();}

}

pommyJavaa at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 5
I dont understand that code block... what are the variables you are using?
IIIIIIlla at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 6
Right now i have a switch statement checking to see wich arrow key is being pressed, then i perform my action based on that, is there a boolean variable that i have access to that might tell me if the key is being held or not in order to eliminate that pause?
IIIIIIlla at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 7
If you are triggering your action with a KeyPressed awt event then you can just wait for the corresponding KeyReleased awt event as the time of release.
Breakfasta at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 8
Currently i have a Thread that waits for a keyPress and a Thread that listens for a keyRelease. should i cut it down to just one Thread? and should i have a while(leftPressed){ do action;} type loop in the keyPressed? or the run.... arg
IIIIIIlla at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 9

here's a simple example for you:

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="Example.class" width=200 height=300></applet> */

public class Example extends Applet implements Runnable, KeyListener {

int w,h,dir,spd;

Rectangle paddle;

Image buffer;

public void init() {

dir = 0; // paddle isn't moving

spd = 2; // speed of the padddle

w = getSize().width;

h = getSize().height;

paddle = new Rectangle(20,0,10,50);

paddle.y = h/2-(paddle.height/2);

buffer = createImage(w,h);

new Thread(this).start();

addKeyListener(this);

}

public void run() {

while (true) {

paddle.y += dir; // move the paddle

/* keep the paddle in bounds */

if (paddle.y < 0) paddle.y = 0;

if (paddle.y > h-paddle.height) paddle.y = h-paddle.height;

repaint();

try {

Thread.sleep(10);

}

catch (InterruptedException e) {

break;

}

}

}

public void paint(Graphics g) {

Graphics2D gfx = (Graphics2D)buffer.getGraphics();

gfx.setColor(Color.white);

gfx.fillRect(0,0,w,h);

gfx.setColor(Color.black);

gfx.drawRect(0,0,w-1,h-1);

gfx.fill(paddle);

g.drawImage(buffer,0,0,this);

}

public void update(Graphics g) {

paint(g);

}

public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_UP:

dir = -spd; // paddle will go up now

break;

case KeyEvent.VK_DOWN:

dir = spd; // paddle will go down now

break;

default:

break;

}

}

public void keyReleased(KeyEvent e) {

int code = e.getKeyCode();

if (code == e.VK_UP || code == e.VK_DOWN) {

dir = 0; // paddle will go nowhere now

}

}

public void keyTyped(KeyEvent e) {}

}

Woogleya at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...
# 10
Thanks, that got it to work
IIIIIIlla at 2007-7-12 10:34:00 > top of Java-index,Other Topics,Java Game Development...