Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java 2D Game: Implementing Simultaneous Keystrokes

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java 2D Game: Implementing Simultaneous Keystrokes

    (I admit to have posted this also in another forum, but I didn't yet get help there and I'm in 'dire' need to solve this problem. Help would be much appreciated. Thank you).

    In my Java 2D game, two tanks are controlled as follows:

    Tank 1

    - Move forward: UP arrow
    - Adjust angle of movement: LEFT and RIGHT arrows

    Tank 2

    - Move forward: W key
    - Adjust angle of movement: A and D keys

    It is possible to adjust the angle of a tank while it's moving forward (e.g., striking the left arrow or the right arrow while striking the up arrow). It's also possible to move both tanks forward simultaneously.

    However, while one tank is moving forward and changing its angle (e.g., striking the up and the left/right key at the same time), the other tank is only able to move forward. That is to say, it cannot adjust its angle (e.g., striking W and A at the same time won't work, while the other tank is doing the same thing; the A key is ignored).

    Why is that? Here's the relevant code:

    In the Board class, that has most of the game logic:

    	while( game_is_running ) {
     
    	    	keysPressed1 = tank1.getKeys();
    	    	keysPressed2 = tank2.getKeys();
     
    	    	if(keysPressed1[0]==true)tank1.setAngle(tank1.getAngle()-3);
    	    	if(keysPressed1[1]==true)tank1.setAngle(tank1.getAngle()+3);
    	    	if(keysPressed1[2]==true){
    	    		tank1.setDX(2 * Math.cos(Math.toRadians(tank1.getAngle())));
    	    		tank1.setDY(2 * Math.sin(Math.toRadians(tank1.getAngle())));
    	    	}
     
    	    	if(keysPressed1[2]==false){
    	    		tank1.setDX(0);
    	    		tank1.setDY(0);
    	    	}
     
    	    	tank1.move();
     
    	    	if(keysPressed2[0]==true)tank2.setAngle(tank2.getAngle()-3);
    	    	if(keysPressed2[1]==true)tank2.setAngle(tank2.getAngle()+3);
    	    	if(keysPressed2[2]==true){
    	    		tank2.setDX( (-1) * ( 2 * Math.cos(Math.toRadians(tank2.getAngle()) ) ));
    	    		tank2.setDY( (-1) * (2 * Math.sin(Math.toRadians(tank2.getAngle()) ) ));
    	    	}
     
    	    	if(keysPressed2[2]==false){
    	    		tank2.setDX(0);
    	    		tank2.setDY(0);
    	    	}
     
    	    	tank2.move();
     
    	    repaint();
        }

    In the Tank class:

        public class Tank extends Entity {
     
        	public Tank(String type){
     
        		this.type = type;
     
        		if(type=="red"){
        			image = new ImageIcon(this.getClass().getResource("sprites/redtank1.PNG")).getImage();
        			x = 200 - image.getWidth(null);
        			y = 400;
        		}
        		if(type=="blue"){
        			image = new ImageIcon(this.getClass().getResource("sprites/bluetank1.PNG")).getImage();
        			x = 850;
        			y = 400;
        		}
     
        		width = image.getWidth(null);
        		height = image.getHeight(null);
     
        		angle = 0;
     
        		dx = 0;
        		dy = 0;
     
        		keysPressed = new boolean[3];
     
        	}
     
        	public void keyPressed(KeyEvent e){
     
        		int key = e.getKeyCode();
     
        		if(key == KeyEvent.VK_LEFT){
        			keysPressed[0] = true;
        			}
     
        		if(key == KeyEvent.VK_RIGHT){
        			keysPressed[1] = true;
        			}
     
        		if(key == KeyEvent.VK_UP){
        			keysPressed[2] = true;
        			}
     
        		if(key == KeyEvent.VK_A){
        			keysPressed[0] = true;
        			}
     
        		if(key == KeyEvent.VK_D){
        			keysPressed[1] = true;
        			}
     
        		if(key == KeyEvent.VK_W){
        			keysPressed[2] = true;
        			}
     
        	}
     
        	public void keyReleased(KeyEvent e){
     
        		int key = e.getKeyCode();
     
        		if(key == KeyEvent.VK_LEFT){
        			keysPressed[0] = false;
        			}
     
        		if(key == KeyEvent.VK_RIGHT){
        			keysPressed[1] = false;
        			}
     
        		if(key == KeyEvent.VK_UP){
        			keysPressed[2] = false;
        			}
     
        		if(key == KeyEvent.VK_A){
        			keysPressed[0] = false;
        			}
     
        		if(key == KeyEvent.VK_D){
        			keysPressed[1] = false;
        			}
     
        		if(key == KeyEvent.VK_W){
        			keysPressed[2] = false;
        			}
     
        	}
     
        }

    You will probably suggest I use Key Bindings, but is there a way to still use KeyListener and have all buttons work at the same time without errors?

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java 2D Game: Implementing Simultaneous Keystrokes

    It sounds like you are running into a hardware limitation of your keyboard, not a software one. There is no possible software fix, the only option is to use a different keyboard.

    Further reading:

    Rollover (key) - Wikipedia, the free encyclopedia
    Microsoft Applied Sciences Group
    How do I remove the limit on PC keyboard button presses? - Arqade

Similar Threads

  1. JPanel not reacting to Keystrokes
    By sonicjr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2012, 06:10 PM
  2. Implementing restartApplication method in Java
    By davidvee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2012, 11:02 AM
  3. Replies: 1
    Last Post: September 15th, 2011, 07:50 AM
  4. Regarding Keystrokes
    By esplanade56 in forum Java Theory & Questions
    Replies: 12
    Last Post: May 25th, 2011, 06:46 AM
  5. Implementing order of operations for a Java calculator.
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2011, 06:11 PM

Tags for this Thread