Key Implication in Java 1.4.2 sdk (Ready to Program)
Author |
Message |
Artimes
|
Posted: Sat Mar 25, 2006 11:29 am Post subject: Key Implication in Java 1.4.2 sdk (Ready to Program) |
|
|
I am creating an asteroids type game with 2 players, got the phyics and the roation and movement yadadada... but it is kind of hard to play a game if you can't input and key values on the keyboard. I have tried the following, does not work all that...
code: | import java.applet.*;
import java.awt.*;
public class Testkeydown extends Applet
{
// Place instance variables here
public boolean keyDown (Event evt, int inkey)
{
Graphics g = getGraphics ();
g.setColor (Color.white);
g.fillRect (1, 1, 200, 30);
g.setColor (Color.red);
g.drawString ("Key: " + (char) (inkey) + " ASCII :" + Integer.toString (inkey), 40, 20);
return true;
}
public void init ()
{
resize (400, 400);
// Place the body of the initialization method here
} // init method
public void paint (Graphics g)
{
// Place the body of the drawing method here
} // paint method
} // Teskeydown class |
You can only have 1 key pressed at a time, and I need multiple keys. At least 2 per player (directional and momental).
I have been researching KeyListener but I can never get it to work in Read to Program (the crappy ass compliler my school uses... and this is a project for school).
I've been using the following, but no luck in 1.4.2 sdk......
code: | import java.applet.*;
import java.awt.*;
public class test extends Applet
{
public void keyPressed (KeyEvent e)
{
switch (e.getKeyCode ())
{
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;
default:
break;
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
} |
My program also must stay an applet, and I was looking but I am not sure what I should import at the top of the program. I know the methods keyPressed, Released, and Typed exist in 1.4.2 sdk and Ready to Program.
A little diretion would be greatly appreciated. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dan
|
Posted: Sat Mar 25, 2006 2:19 pm Post subject: (No subject) |
|
|
I whould look in to keylisteners. Witch looks kind of like what you are doing but with RTP that has some odditys to it.
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
Now how to get around only one keyinput at a time whould be to use keyPressed and keyReleased to check for both keys being down. So rather then using a case use if statmens like:
code: |
if (e.getKeyCode() = KeyEvent.VK_UP)
do somthing
if (e.getKeyCode() = KeyEvent.VK_LEFT)
do somthing eltes
|
|
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
|
|