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

    Default Java 2D Game: Implementing Simultaneous Keystrokes

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

    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:

    Java Code:
    	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:

    Java Code:
        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
    Last edited by AvivC; 01-01-2014 at 08:22 PM.

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default Re: Java 2D Game: Implementing Simultaneous Keystrokes

    I'm not sure if this would work but you could give it a shot...
    I would try doing an and statement in one of your ifs to check if the tank is moving and if they are changing the angle, then just update them both in the if statement. Once again, I am not entirely sure this will work, but maybe give it a try.

Similar Threads

  1. RFC: Design for simultaneous socket connections
    By blackbird in forum Networking
    Replies: 6
    Last Post: 07-20-2011, 04:31 PM
  2. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  3. Send Keystrokes from Java to Notepad
    By ramyal in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2010, 06:09 PM
  4. Replies: 24
    Last Post: 12-03-2009, 10:58 PM
  5. Doubt in simultaneous 'implementation' and 'extension'
    By ajaygargnsit in forum New To Java
    Replies: 2
    Last Post: 12-20-2007, 09:33 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
  •