Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Faster smoother input

Started by sakky Dec 18, 2002 at 1:22 PM 0 replies 300+ views
Original Post
sakky
sakky
I get really jerky input from my applet. this is my code public boolean keyDown( Event e, int key ) { if ( key == Event.LEFT ) { x = x - 4; } if ( key == Event.RIGHT ) { x = x + 4; } if ( key == Event.DOWN ) { y = y + 4; } if ( key == Event.UP ) { y = y - 4; } if ( key == Event.ENTER ) { } return ( true ); } I use doule buffering and I''m having an animated gif moving along the screen. I found it easier to make the gifs animated because Java automaticly animated gifs that have frames in them. So I get more animation for less code work. Is this a bad thing? Can it be that the animated gifs are slowing the applet''s input down? Is there a way I can speed the input up?
Take back the internet with the most awsome browser around, FireFox
Abob
Abob
To get better response you have to use something like this


  

import java.awt.*;
import java.awt.event.*;

public class CKeyStates implements KeyListener {
private boolean[] keys = new boolean[256];
CKeyStates() {}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
keys[keyCode] = true;
}

public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
keys[keyCode] = false;
}

public void keyTyped(KeyEvent e) {} // Do nothing


public boolean IsPressed(int keyCode)
{
if ((keyCode < 0) || (keyCode >= 256)) {
return false;
}
return keys[keyCode];
}

}




to use the class do this

//make one
CkeyStates keyboard = new CkeyStates();

// attach It to the applet
addKeyListener(keyboard);

// use it
if (keyboard. IsPressed(‘a’)) {
System.out.println(“A is down”);
}


With this class to get the arrow keys, you have to use the constants

VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT

Not Event.RIGHT etc.

hope this helps
Arrg!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.