Results 1 to 7 of 7
  1. #1
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Delay using Key Bindings

    (Once again, this has been posted in another forum, but I don't get help there and I really need to solve. I hope you understand and I'll appreciate your help).

    I got Key Bindings for a game I'm working on. They're supposed to move objects when keys are pressed.

    (currently, each println(..) represents actual code to move things on the screen, will be added later).

    The printing in the console works, but with delays. The words "left", "right", etc. appear with a delay in the console, sometimes half a second delay, sometimes a few seconds delay.

    There is no delay at all sometimes, but when a lot of keys are pressed one after the other, there's a delay of several seconds, and only then the words suddenly appear in the console (like the computer had too much to handle at the same time).

    When I did the same Key Bindings in a different project without all the game logic, only pressing buttons that print words in the console - it worked perfectly without delay.

    So I suspect the problem is something with the game code, or the way I used Key Bindings inside the game code.

    How do I fix this?

    I tried putting the Key Bindings code in the thread that contains the game-loop (before the game-loop), and in the constructor. Same problem.

    Here is the relevant code of the Board class (extending JPanel), the main JPanel of the game which displays the graphics and manipulates objects.

    Java Code:
        import java.util.*;
        import java.awt.*;
        import java.awt.event.*;
        
        import javax.swing.*;
        
        public class Board extends JPanel implements Runnable {
        
        	Tank tank1,tank2;
        	boolean[] keysPressed1,keysPressed2;
        	Action upAction,leftAction,rightAction,wAction,aAction,dAction;
        	InputMap inputMap;
        	ActionMap actionMap;
        	
        	public Board(){
        		
        		setFocusable(true);
        		setBackground(Color.BLACK);
        		
        		tank1 = new Tank("red");
        		tank2 = new Tank("blue");
        		
        		inputMap = this.getInputMap();
        		actionMap = this.getActionMap();
        		
        		Thread gameloop = new Thread(this);
        		gameloop.start();
        		
        	}
        	
        	public void paintComponent(Graphics g){
        		
        		super.paintComponent(g);
        		Graphics2D g2d = (Graphics2D) g;
        		
        		// Drawing tanks on the screen.
        		
        	}
        	
        	public void run(){
        		
        		// Key Bindings //////
        		
        		upAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("up");
        			}
        		};
        		
        		leftAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("left");
        			}
        		};
        		
        		rightAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("right");
        			}
        		};
        		
        		wAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("w");
        			}
        		};
        
        		aAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("a");
        			}
        		};
        		
        		dAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        				System.out.println("d");
        			}
        		};
        		
        		inputMap.put(KeyStroke.getKeyStroke("UP"),"upAction");
        		inputMap.put(KeyStroke.getKeyStroke("LEFT"),"leftAction");
        		inputMap.put(KeyStroke.getKeyStroke("RIGHT"),"rightAction");
        		inputMap.put(KeyStroke.getKeyStroke("W"),"wAction");
        		inputMap.put(KeyStroke.getKeyStroke("A"),"aAction");
        		inputMap.put(KeyStroke.getKeyStroke("D"),"dAction");
        		
        		actionMap.put("upAction",upAction);
        		actionMap.put("leftAction",leftAction);
        		actionMap.put("rightAction",rightAction);
        		actionMap.put("wAction",wAction);
        		actionMap.put("aAction",aAction);
        		actionMap.put("dAction",dAction);
        		
                    // End Key Bindings //////
                    
                    // Start of game loop. ////
    
        		int TICKS_PER_SECOND = 50;
        		int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
        		int MAX_FRAMESKIP = 10;
        
        		long next_game_tick = System.currentTimeMillis();
        		int loops;
        
        		boolean game_is_running = true;
        		
        		while( game_is_running ) {
        
        		    loops = 0;
        		    while( System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) {
        		    	
                            // Code to manipulate tank1.
                            // .......
    
        		    	tank1.move();
        		    	
                            // Code to manipulate tank2.
                            // .......
    
        		    	tank2.move();
        
        		        next_game_tick += SKIP_TICKS;
        		        loops++;
        		    }
        
        		    repaint();
        		}
        	
        	}
        }
    As I said, I suspect the program demands too much of the computer, so pressing a lot of keys one after the other, freezes the program for several seconds, and then suddenly prints the words on the console.

    Thank you for the help

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Delay using Key Bindings

    I would suggest putting the Key Bindings in a separate thread from the game itself.

  3. #3
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: Delay using Key Bindings

    Tried it, doesn't work :\

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Delay using Key Bindings

    Huh. Is there a certain reason you chose Key Bindings over KeyListener?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Delay using Key Bindings

    Please post an SSCCE (see signature) to demonstrate the problem. In some cases we need to experience the problem before we can assist. If your going to post code, please post code that compiles (which means providing all classes outside the JDK). But do not post your entire program.

    Regards,
    Jim
    Last edited by jim829; 01-03-2014 at 05:57 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: Delay using Key Bindings

    I'll post an SSCCE soon.

    In the meantime, please answer this: Could it be that the println(...) commands in the actionPerformed() methods, are running in the EDT because they are event handlers, and thus are delayd because the EDT also does all the repainting of the tanks on the screen? (does it?)

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Delay using Key Bindings

    Quote Originally Posted by AvivC View Post
    I'll post an SSCCE soon.

    In the meantime, please answer this: Could it be that the println(...) commands in the actionPerformed() methods, are running in the EDT because they are event handlers, and thus are delayd because the EDT also does all the repainting of the tanks on the screen? (does it?)
    There is no way in my mind that it is the println lines. I think if its anything, its the multiple Actions that are associated with Key Bindings.

Similar Threads

  1. Key Bindings Vs. KeyListener
    By AvivC in forum New To Java
    Replies: 8
    Last Post: 01-02-2014, 05:31 PM
  2. My key bindings don't work...
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2013, 11:28 PM
  3. Unsure of whether to use key bindings
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2012, 08:33 AM
  4. can one disable key bindings on a JComponent?
    By gib65 in forum AWT / Swing
    Replies: 2
    Last Post: 10-14-2010, 05:44 PM
  5. genesis 3.0-RC2: Swing, SWT and Thinlet bindings
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 11:12 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
  •