Results 1 to 5 of 5
  1. #1
    evared is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Post Keylistener not working when screen is constantly refreshed

    Well basically i am making a game, and in this game you obviously need a keyboard input as you do every other game. To test the initial keyboard input and check everything is working in that respect i have the escape button setup to call System.exit(). This works fine on all of the menu's, but when i start the actual game, load the maps etc, it doesn't call at all. I have looked over and revised a lot of code, but there are actually no changes to anything wen moving from the menu's to game. When you click a button and it takes you to another menu, it calls a method called wipe(boolean,boolean), the boo's decide whether to remove the background image and logo. At the top of the wipe method, all of the buttons are removed to make way for the new buttons.

    When the game is initiated, a method called gameLoop() is called which then checks what to display on the screen updates sprites etc. But when this method is called the Keyboard is completely non responsive. I have tried using keymaps, changing the focusable boolean of every component and even changing what component is painted on the screen, e.g. getFullScreenWindow() in graphics environment, and the actual JFrame, and JPanel's. Nothing seems to change this.

    Java Code:
    public void gameLoop(){
    		long a = 0;
    		long ttlTime = 0;
    		long startTime = System.currentTimeMillis();
    		long currTime = startTime;
    		int m1 = 1;
    		int m2 = 1;
    		int m3 = 1;
    		int m4 = 1;
    		int m5 = 1;
    		int m6 = 1;
    		int m7 = 1;
    		int m8 = 1;
    		int m9 = 1;
    		int m10 = 1;
    		int m11 = 1;
    		int m12 = 1;
    		int m13 = 1;
    		int m14 = 1;
    		int m15 = 1;
    		int m16 = 1;
    		int m17 = 1;
    		int m18 = 1;
    		while(getGameMode() == GameMode.SINGLE_PLAYER_IN_GAME){
    
    			long elapsedTime = System.currentTimeMillis() - currTime;
    			currTime += elapsedTime;
    			
    			updateAnimations(elapsedTime);
               	getCurrentMap().centerScreenToPlayer(this,getCurrentPlayer());
    			
                this.displayCurrentMapGraphics();
    			
    			
    			sleep(5);
    			
    			ttlTime += elapsedTime;
    			
    			if(ttlTime >= 20000){
    			    System.exit(1);
    			}
    			double mt = (m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11+m12+m13+m14+m15+m16+m17+m18)/18;
    	                m2 = m1;
    			m3 = m2;
    			m4 = m3;
    			m5 = m4;
    			m6 = m5;
    			m7 = m6;
    			m8 = m7;
    			m9 = m8;
    			m10 = m9;
    			m11 = m10;
    			m12 = m11;
    			m13 = m12;
    			m14 = m13;
    			m15 = m14;
    			m16 = m15;
    			m17 = m16;
    			m18 = m17;
    			m1 = (int) elapsedTime;
    			this.runtime += elapsedTime;
    			if(a < 1000){
    				a += elapsedTime;
    			}else{
    				this.fps = (int) (1000/mt);
    				a = 0;
    			}
    		}
    		
    	}
    The game engine is very far along, but i cant get past this.

    If anyone would like to see any code, just request it and i would be more than happy to put it in this top part. Any help would be VERY appreciated, cheers, Paul.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Keylistener not working when screen is constantly refreshed

    You're likely locking up the Swing event thread with this loop if it is being called on the thread. Since this thread is responsible for all Swing graphics and user interaction, tying it up will effectively freeze your application. Are you taking care to use background threading? Should you instead use a Swing Timer? Or a SwingWorker object?

  3. #3
    evared is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Keylistener not working when screen is constantly refreshed

    So use a Swing Timer or Worker, to update the screen instead of my own loop? That should work, thanks :)
    Last edited by evared; 08-17-2012 at 06:56 PM.

  4. #4
    evared is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Keylistener not working when screen is constantly refreshed

    Well i implemented a new class that handles the game loop using a timer. i basically implemented the loop but in another class using ActionEvents etc. This caused a massive problem. The screen was flashing white, the menu's weren't going and were being displayed in the background when the actual game was running, and worst of all, the keyboard still didn't work :(
    I also forgot to say before that the current script i use, runs very smoothly, at around 60fps, the most I've got out of it is around 75fps.
    Last edited by evared; 08-17-2012 at 06:59 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Keylistener not working when screen is constantly refreshed

    I'd say that something is not working right, that you've implemented your Timer wrong.

    If you need better help, consider giving more information and showing code with code tags.

Similar Threads

  1. KeyListener not working
    By cc11rocks in forum AWT / Swing
    Replies: 3
    Last Post: 02-09-2011, 01:09 PM
  2. keylistener + applet isn't working
    By brandon95 in forum Java Applets
    Replies: 6
    Last Post: 01-03-2011, 02:10 PM
  3. JLabel is not refreshed from withing ActionPerformed
    By mavaforumsm in forum AWT / Swing
    Replies: 2
    Last Post: 05-25-2010, 05:32 PM
  4. Full-Screen Window is not supporting KeyListener
    By AnOdeToCode in forum Java 2D
    Replies: 1
    Last Post: 02-13-2009, 08:08 PM
  5. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 AM

Tags for this Thread

Posting Permissions

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