Results 1 to 2 of 2
  1. #1
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Can't figure out how to create a keyboard class for lwjgl

    I'm very new to java. I'm trying to make a very basic game engine as a way to help me learn the language. Of course, before I can make an infinite loop, I have to know how to get input from the keyboard so that I can break out of it.

    Unfortunately, that's where the problem is. I can't seem to get a key-press input to work. I'm getting an error saying:
    Java Code:
    Caused by: java.lang.IllegalStateException: Keyboard must be created before you can query key state
    	at org.lwjgl.input.Keyboard.isKeyDown(Keyboard.java:398)
    	at Runner.<clinit>(Runner.java:8)
    I've tried making a new variable and setting it to "new Mouse();", but that didn't do anything. Can anyone help me?

    Here is my Runner.class:

    //imports
    import org.lwjgl.input.Keyboard;
    import org.lwjgl.input.Mouse;

    Java Code:
    public class Runner {
    	//declare variables
    	public static General_Mob Mob1 = new General_Mob(); //create a new instance of General_Mob called "Mob1"
    	static boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_X);
    	
    	//methods
    	public static void main(String[] args){
    		System.out.println("Check.  Check.  Check.");
    		Mob1.Create();
    		for (;keyDown;){
    		goAround(Mob1);
    		}
    		}
    	
    	public static void goAround(General_Mob Mob1){
    		Mob1.goAround();
    		
    	}
    
    }
    And in case my "General_Mob" class is causing some sort of error, here's the source for that too:
    Java Code:
    //imports
    import java.lang.Math;
    import org.lwjgl.input.Keyboard;
    import org.lwjgl.input.Mouse;
    
    public class General_Mob {
    	
    	//declare variables
    	private static double x, y, z, vspeed, hspeed, zspeed, friction;
    	public static int xround, yround, zround;
    
    	//methods
    	public static void Create() {
    		x = 0;
    		y = 0;
    		vspeed=3;
    		hspeed=2;
    		zspeed=1;
    		friction=2;
    
    	}
    	
    	public static void goAround(){
    		//First things first: Update the mob's position, speed, and friction.
    		crunchPhysics();
    		System.out.println("X:" + xround + " Y:" + yround + " Z:" + zround);
    	}
    	
    	
    	public static void crunchPhysics(){
    				//Slow the mob down based on its friction
    				if (hspeed <=0){
    					hspeed+=friction;
    				}
    				else{
    					hspeed-=friction;
    				}
    				
    				if (vspeed <=0){
    					vspeed+=friction;
    				}
    				else{
    					vspeed-=friction;
    				}
    				if (zspeed <=0){
    					zspeed+=friction;
    				}
    				else{
    					zspeed-=friction;
    				}
    				
    				//Move the mob based on its speed.
    				x += hspeed;
    				y += vspeed;
    				z += zspeed;
    				
    				
    				//Round the positions to integers so that they can be drawn on the screen.  After all, how can you draw something that's .01 pixels?
    				xround=(int) Math.rint(x);
    				yround=(int) Math.rint(y);
    				zround=(int) Math.rint(z);
    				
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't figure out how to create a keyboard class for lwjgl

    Can you rewrite your programs to use the java SE classes? Not many people have the third party packages and classes you are using.
    Or is that what you are trying to learn?

Similar Threads

  1. LWJGL Collision Detection
    By Furyright in forum Java 2D
    Replies: 1
    Last Post: 10-09-2011, 06:48 AM
  2. lwjgl help PLEASE!
    By Brandon in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 06:11 AM
  3. Replies: 10
    Last Post: 04-20-2011, 02:03 AM
  4. LWJGL: Camera + Block Collision
    By Aaron in forum New To Java
    Replies: 6
    Last Post: 11-14-2010, 09:53 PM
  5. Keyboard Input In A Seperate Class?
    By FatalSylence in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 05:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •