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 7 of 7

Thread: Keyboard Listeners to Control object states

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Keyboard Listeners to Control object states

    I am trying to develop a simple 2D gaming engine. The latest bug I have come across involves the keyboard input not correctly changing the proper variables and the display output is getting confused on what to display and where. I have boolean values set for moveLeft and moveRight, which are supposed to be set to true if the left or right arrows are being pressed. If the value is true, there is supposed to be a check for each direction, in the form of checkWest, checkEast, etc... which returns how far a game object can move. Somewhere in the mix I messed something up and cannot figure out what exactly.

    Also, please be sure to note that my code is far from efficient and needs to be optimized. I am trying to get the key functions working before I start simplifying the code, which is currently messy at best.

    package Mark3;
     
    import java.awt.BorderLayout;
     
     
     
     
     
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.Scanner;
     
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.Timer;
     
     
     
     
     
    public class GameTester extends JFrame{
    	//tester variables
    	private KeyboardPanel keyboardPanel;
    	private static Player player;
    	private static PWeapon[] pWeapon;
    	private static Enemy[] enemy;
    	private static EWeapon[] eWeapon;
    	private static Environment[] environment;
    	private static Image bg0;
    	private static Image bg1;
    	private static int[] level;
    	private static int[] levelDim;
    	private static JFrame frame;
    	private static String theme;
    	private static float[] playerPoint;
     
    	//temp variables
    	private static int[] tempIntA0;
    	private static int[] tempIntA1;
    	private static int tempInt0;
    	private static int tempInt1;
    	private static float[] tempFloA0;
    	private static float[] tempFloA1;
    	private static float tempFlo0;
    	private static float tempFlo1;
     
    	//extras
     
     
    	public KeyboardPanel getKeyboardPanel() {
    		return keyboardPanel;
    	}
    	public void setKeyboardPanel(KeyboardPanel keyboardPanel) {
    		this.keyboardPanel = keyboardPanel;
    	}
    	public static Player getPlayer() {
    		return player;
    	}
    	public static void setPlayer(Player player) {
    		GameTester.player = player;
    	}
    	public static PWeapon[] getpWeapon() {
    		return pWeapon;
    	}
    	public static void setpWeapon(PWeapon[] pWeapon) {
    		GameTester.pWeapon = pWeapon;
    	}
    	public static Enemy[] getEnemy() {
    		return enemy;
    	}
    	public static void setEnemy(Enemy[] enemy) {
    		GameTester.enemy = enemy;
    	}
    	public static EWeapon[] geteWeapon() {
    		return eWeapon;
    	}
    	public static void seteWeapon(EWeapon[] eWeapon) {
    		GameTester.eWeapon = eWeapon;
    	}
    	public static Environment[] getEnvironment() {
    		return environment;
    	}
    	public static void setEnvironment(Environment[] environment) {
    		GameTester.environment = environment;
    	}
    	public static Image getBg0() {
    		return bg0;
    	}
    	public static void setBg0(Image bg0) {
    		GameTester.bg0 = bg0;
    	}
    	public static Image getBg1() {
    		return bg1;
    	}
    	public static void setBg1(Image bg1) {
    		GameTester.bg1 = bg1;
    	}
    	public static int[] getLevel() {
    		return level;
    	}
    	public static void setLevel(int[] level) {
    		GameTester.level = level;
    	}
    	public static int[] getLevelDim() {
    		return levelDim;
    	}
    	public static void setLevelDim(int[] levelDim) {
    		GameTester.levelDim = levelDim;
    	}
    	public static int[] getTempIntA0() {
    		return tempIntA0;
    	}
    	public static void setTempIntA0(int[] tempIntA0) {
    		GameTester.tempIntA0 = tempIntA0;
    	}
    	public static int[] getTempIntA1() {
    		return tempIntA1;
    	}
    	public static void setTempIntA1(int[] tempIntA1) {
    		GameTester.tempIntA1 = tempIntA1;
    	}
    	public static int getTempInt0() {
    		return tempInt0;
    	}
    	public static void setTempInt0(int tempInt0) {
    		GameTester.tempInt0 = tempInt0;
    	}
    	public static int getTempInt1() {
    		return tempInt1;
    	}
    	public static void setTempInt1(int tempInt1) {
    		GameTester.tempInt1 = tempInt1;
    	}
    	public static float[] getTempFloA0() {
    		return tempFloA0;
    	}
    	public static void setTempFloA0(float[] tempFloA0) {
    		GameTester.tempFloA0 = tempFloA0;
    	}
    	public static float[] getTempFloA1() {
    		return tempFloA1;
    	}
    	public static void setTempFloA1(float[] tempFloA1) {
    		GameTester.tempFloA1 = tempFloA1;
    	}
    	public static float getTempFlo0() {
    		return tempFlo0;
    	}
    	public static void setTempFlo0(float tempFlo0) {
    		GameTester.tempFlo0 = tempFlo0;
    	}
    	public static float getTempFlo1() {
    		return tempFlo1;
    	}
    	public static void setTempFlo1(float tempFlo1) {
    		GameTester.tempFlo1 = tempFlo1;
    	}
     
    	public static void main(String[] args){
     
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("What theme?"); //menu for theme
    		System.out.println("1) Green");
    		System.out.println("2) Blue");
    		System.out.println("3) Brown");
     
    		int TSelect = input.nextInt(); //input from menu
     
    		System.out.println("What is your name?");
     
    		String name = input.toString();
     
    		player = new Player(name);
     
    		pWeapon = new PWeapon[0];
     
    		eWeapon = new EWeapon[0];
     
    		enemy = new Enemy[0];
     
    		//setup frame
    		frame = new GameTester();
    		frame.setTitle("Tester");
    		frame.setSize(600, 500);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
    		//following code is specific to building the level
     
    		switch (TSelect) { //switch to select theme
     
    		case 1: theme = "Green";
    		break;
     
    		case 2: theme = "Blue";
    		break;
     
    		case 3: theme = "Brown";
    		}
     
    		//set a switch to choose different levels for future code
     
    		int[][] layout = getMap0(); //array for level layout
     
    		levelDim = new int[] {layout.length * 50, layout[1].length * 50 }; //generate dimension of level
     
    		//setup starting point for Map0
    		level = new int[] {0, -150};
     
    		tempInt0 = 0; //reset temp variables
     
     
    		for (int i = 0; i < layout.length; i++){ //build environment array length
    			for (int j = 0; j < layout[1].length; j++){
    				if (layout[i][j] != 0){ //if (0 < layout[i][j] < 20); if 20 types of environment objects
    					tempInt0++;
    				}
    			}		
    		}
     
    		Environment[] tempEnvironment = new Environment[tempInt0]; //temp environment array 
     
    		tempInt1 = 0; //reset temp variables
     
     
    		for (int i = 0; i < layout.length; i++){ //build 1st dimension
    			for (int j = 0; j < layout[1].length; j++){ //build 2nd dimension
    				if (layout[i][j] == 1){
    					float point[] = {(float) (i * 50.0), (float) ((j * 50.0) + level[1])};
    					tempEnvironment[tempInt1] = new Environment("basicBlock", point);  //basicBlock constructors 
    					tempInt1++; 
    				}
    				if (layout[i][j] == 2){
    					float point[] = {(float) (i * 50.0), (float) (j * 50.0)};
    					tempEnvironment[tempInt1] = new Environment("metalBlock", point); //metalBlock constructors
    					tempInt1++;
    				}
    				//add in constructors for other environmental variables
     
    				//add in constructors for enemies
     
    			}
     
    		}
    		environment = tempEnvironment; 
    		//enemy = tempEnemy
    	}
     
    	public GameTester(){
    		//game tester constructor, sets up layout manager for frame
    		String vp = "null";
    		setLayout(new BorderLayout());
     
    		JPanel p1 = new AnimatedPanel(vp);
    		p1.setSize(600, 500);
     
    		JPanel p2 = new KeyboardPanel();
    		p2.setSize(0, 0);
     
     
    		add(p1, BorderLayout.CENTER);
     
    		add(p2, BorderLayout.SOUTH);
    		p2.setFocusable(true);
    	}
     
    	static class AnimatedPanel extends JPanel {
     
    		//player images
    		ImageIcon playerRight = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/NinjaRight.gif");
    		Image playerRight2 = playerRight.getImage();
     
    		ImageIcon playerLeft = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/NinjaLeft.gif");
    		Image playerLeft2 = playerLeft.getImage();
     
    		ImageIcon playerThrowLeft = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/ninjaThrowLeft.gif");
    		Image playerThrowLeft2 = playerThrowLeft.getImage();
     
    		ImageIcon playerThrowRight = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/ninjaThrowRight.gif");
    		Image playerThrowRight2 = playerThrowRight.getImage();
     
    		ImageIcon shuriken0A = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/shuriken0.gif");
    		Image shuriken0 = shuriken0A.getImage();
     
    		ImageIcon shuriken1A = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/shuriken1.gif");
    		Image shuriken1 = shuriken1A.getImage();
     
    		ImageIcon blueBlock = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/blue.gif");
    		Image blueBlock2 = blueBlock.getImage();
     
    		ImageIcon brownBlock = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/brown.gif");
    		Image brownBlock2 = brownBlock.getImage();
     
    		ImageIcon greenBlock = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/green.gif");
    		Image greenBlock2 = greenBlock.getImage();
     
    		ImageIcon metalBlock = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/metal.gif");
    		Image metalBlock2 = metalBlock.getImage();
     
    		ImageIcon background = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Mark3/background.gif");
    		Image background2 = background.getImage();
     
    		public AnimatedPanel(String message){
    			Timer timer = new Timer(62, new TimerListener());
    			timer.start();
    		}
     
    		protected void paintComponent(Graphics g){
    			super.paintComponent(g);
     
    			//g.draw image(which image, # of pixels from left border, # of pixels from top border, width of image, height of image, this??)
    			//draw in order of display, with background first, then level, then player
     
    			g.drawImage(background2, level[0], level[1], levelDim[0] / 3, levelDim[1] * 3, this); //draw background image
     
     
    			for (int i = 0; i < environment.length; i++){
     
    				float[] temp = environment[i].getPoint();	//check location
    				if (temp[0] > 0){ 							//check to see if on screen
    					if (temp[0] < frame.getWidth()){
    						if (temp[1] > 0){
    							if (temp[1] < frame.getHeight()){
     
    								if (environment[i].metal = false){
    									//add in false check for platinum
    									if (theme == "Green"){
     
    										g.drawImage(greenBlock2, (int) temp[0], (int) temp[1], 50, 50, this);
    									}
     
    									if (theme == "Blue"){
    										g.drawImage(blueBlock2, (int) temp[0], (int) temp[1], 50, 50, this);
    									}
     
    									if (theme == "Brown"){
    										g.drawImage(brownBlock2, (int) temp[0], (int) temp[1], 50, 50, this);
    									}
    								}
    								if (environment[i].metal = true){
    									g.drawImage(metalBlock2, (int) temp[0], (int) temp[1], 50, 50, this);
    								}	
    								//add in true check for platinum
    							}
    						}
    					}
    				}
    			}
     
    			if (player.faceRight = true){ //draw player image
     
    				if (player.throwShuriken = true){
    					g.drawImage(playerThrowRight2, (int) player.point[0], (int) player.point[1], 70, 90, this);
    				}
    				if (player.throwShuriken = false){
    				//something going on with the draw image constructor variables
     
     
    					g.drawImage(playerRight2, (int) player.point[0], (int) player.point[1], 70, 90, this);
    				}
    			}
    			if (player.faceRight = false){
     
    				if (player.throwShuriken = true){
    					g.drawImage(playerThrowLeft2, (int) player.point[0], (int) player.point[1], 70, 90, this);
    				}
    				if (player.throwShuriken = false){
    					g.drawImage(playerLeft2, (int) player.point[0], (int) player.point[1], 70, 90, this);
    				}
     
     
    			}
     
    			//draw enemy objects
    			for (int i = 0; i < enemy.length; i++){
     
    			}
     
    			//draw player weapons
    			for (int i = 0; i < pWeapon.length; i++){
     
    			//checks for weapon type
    				// if (pWeapon.type == blah blah blah
    				for (int j = 0; j < pWeapon[i].animation.length; j++){
    					if (j == 0){
    						g.drawImage(shuriken0, (int) pWeapon[i].point[0], (int) pWeapon[i].point[1], 40, 30, this);
    					}
    					if (j == 1){
    						g.drawImage(shuriken1, (int) pWeapon[i].point[0], (int) pWeapon[i].point[1], 40, 30, this);
    					}				
    					if (j == pWeapon[i].animation.length){
    						j = 0;
    					}
    				}
    			}
     
    			//draw enemy weapons
    			for (int i = 0; i < eWeapon.length; i++){
     
    			}
     
    		}
     
    		class TimerListener implements ActionListener{
    			//setup various checks here
    			//for example, gravity
     
    			public void actionPerformed(ActionEvent e){
    				//or setup various checks here
     
    				//checks for player functions
     
    			if (player.moveLeft = true){
    				//checkWest(int velocity, float[] objBox){
    				float[] temp2 = player.getHitbox();
    				float[] temp3 = player.getPoint();
     
    				int temp = checkWest(-8, temp2);
    				if (temp == 0){
    					player.setMoveLeft(false);
    				}
     
    				temp2[0] = temp2[0] + temp;
    				temp2[1] = temp2[1] + temp;
    				temp3[0] = temp3[0] + temp;
    				player.setHitbox(temp2);
    				player.setPoint(temp3);
     
    			}
     
    			if (player.moveRight = true){
    				//checkWest(int velocity, float[] objBox){
    				float[] temp2 = player.getHitbox();
    				float[] temp3 = player.getPoint();
     
    				int temp = checkWest(8, temp2);
    				if (temp == 0){
    					player.setMoveLeft(false);
    				}
     
    				temp2[0] = temp2[0] + temp;
    				temp2[1] = temp2[1] + temp;
    				temp3[0] = temp3[0] + temp;
    				player.setHitbox(temp2);
    				player.setPoint(temp3);
     
    			}
     
     
    				repaint();
    			}		
    		}
    	}
     
    	static class KeyboardPanel extends JPanel {
     
    		public KeyboardPanel(){
    			addKeyListener(new KeyAdapter(){
    				public void keyPressed(KeyEvent e){
    					switch (e.getKeyCode()){
     
    						case KeyEvent.VK_DOWN:
    							//info for going down ladder, etc..
     
    						case KeyEvent.VK_UP:
    							//info for going up ladder, etc...
     
    						case KeyEvent.VK_LEFT:
     
    							player.moveLeft = true;	
    							if (player.faceRight = true){
    								player.setFaceRight(false);
    							}
     
    						case KeyEvent.VK_RIGHT:
     
    							player.moveRight = true;
    							if (player.faceRight = false){
    								player.setFaceRight(true);
    							}
     
    						case KeyEvent.VK_SPACE:
     
    							player.setJump(true);
     
    						case KeyEvent.VK_F:
     
    							if (pWeapon.length < 3){
    								PWeapon[] temp = new PWeapon[pWeapon.length + 1];
    								for (int i = 0; i < pWeapon.length; i++){
    									temp[i] = pWeapon[i];
    								}
    								float[] temp2 = player.getPoint();
     
    								if (player.faceRight = true){
     
    									temp2[0] = temp2[0] + 40;
    									temp2[1] = temp2[1] + 40;
    									PWeapon temp3 = new PWeapon("Shuriken", temp2, "right");	
    									temp[temp.length - 1] = temp3;
    								}
    								if (player.faceRight = false){
     
    									temp2[1] = temp2[1] + 40;
    									PWeapon temp3 = new PWeapon("Shuriken", temp2, "left");	
    									temp[temp.length - 1] = temp3;
    								}
     
    								pWeapon = temp;
     
    							}
     
    					}
     
    				}
     
    				public void keyReleased(KeyEvent e){
    					switch (e.getKeyCode()){
     
    					case KeyEvent.VK_LEFT:
    						player.moveLeft = false;
     
    					case KeyEvent.VK_RIGHT:
    						player.moveRight = false;
     
    					case KeyEvent.VK_SPACE:
    						player.setJump(false);
    					}
     
     
    				}
    			});
    		}
     
    	}
     
    	public int checkNorth(int velocity, float[] objBox){
    		int temp = velocity; //value for number of pixels to move
    		for (int i = 0; i < environment.length; i++){ //checks each environment object
    			if (environment[i].solid = true){ //ignores environment objects that are not solid
    				float[] checkBox = environment[i].getHitbox(); //gets hitbox of environment
    				for (int temp2 = 0; temp2 > velocity; temp2--){ //sets range for distance traveled
    					for (int j = (int)objBox[0]; j < objBox[1]; j++){ //checks north hitbox of the user/enemy object
    						for (int k = (int)checkBox[0]; k < checkBox[1]; k++){ //checks south hitbox of environment object
    							if (k == j && checkBox[3] == objBox[2]){ //checks to see if objects collide
    								if (temp2 > temp){ //checks if range is shorter than velocity
    									temp = temp2; //replaces range if shorter than velocity
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return temp; //returns range to move
     
    	}
     
    	public int checkSouth(int velocity, float[] objBox){
    		int temp = velocity; //value for number of pixels to move
    		for (int i = 0; i < environment.length; i++){ //checks each environment object
    			if (environment[i].solid = true){ //ignores environment objects that are not solid
    				float[] checkBox = environment[i].getHitbox(); //gets hitbox of environment
    				for (int temp2 = 0; temp2 < velocity; temp2++){ //sets range for distance traveled
    					for (int j = (int)objBox[0]; j < objBox[1]; j++){ //checks south hitbox of the user/enemy object
    						for (int k = (int)checkBox[0]; k < checkBox[1]; k++){ //checks north hitbox of environment object
    							if (k == j && checkBox[2] == objBox[3]){ //checks to see if objects collide
    								if (temp2 < temp){ //checks if range is shorter than velocity
    									temp = temp2; //replaces range if shorter than velocity
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return temp; //returns range to move
    	}
     
    	public static int checkWest(int velocity, float[] objBox){
    		int temp = velocity; //value for number of pixels to move
    		for (int i = 0; i < environment.length; i++){ //checks each environment object
    			if (environment[i].solid = true){ //ignores environment objects that are not solid
    				float[] checkBox = environment[i].getHitbox(); //gets hitbox of environment
    				for (int temp2 = 0; temp2 > velocity; temp2--){ //sets range for distance traveled
    					for (int j = (int)objBox[2]; j < objBox[3]; j++){ //checks West hitbox of the user/enemy object
    						for (int k = (int)checkBox[2]; k < checkBox[3]; k++){ //checks East hitbox of environment object
    							if (k == j && checkBox[1] == objBox[0]){ //checks to see if objects collide
    								if (temp2 > temp){ //checks if range is shorter than velocity
    									temp = temp2; //replaces range if shorter than velocity
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return temp; //returns range to move
     
    	}
     
    	public int checkEast(int velocity, float[] objBox){
    		int temp = velocity; //value for number of pixels to move
    		for (int i = 0; i < environment.length; i++){ //checks each environment object
    			if (environment[i].solid = true){ //ignores environment objects that are not solid
    				float[] checkBox = environment[i].getHitbox(); //gets hitbox of environment
    				for (int temp2 = 0; temp2 < velocity; temp2++){ //sets range for distance traveled
    					for (int j = (int)objBox[2]; j < objBox[3]; j++){ //checks East hitbox of the user/enemy object
    						for (int k = (int)checkBox[2]; k < checkBox[3]; k++){ //checks West hitbox of environment object
    							if (k == j && checkBox[0] == objBox[1]){ //checks to see if objects collide
    								if (temp2 < temp){ //checks if range is shorter than velocity
    									temp = temp2; //replaces range if shorter than velocity
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return temp; //returns range to move
    	}
     
    	public boolean checkHitbox(int[] velocity, int[] objBox, int[] checkBox){
    		int[] tempBox = {objBox[0] + velocity[0], objBox[1] + velocity[0], objBox[2] + velocity[1], objBox[3] + velocity[1]}; //sets up temp hitbox to check next location
    		for (int i = tempBox[0]; i < tempBox[1]; i++){ //checks x variables on tempBox
    			for (int j = tempBox[2]; j < tempBox[3]; j++){ //checks y variables on tempBox
    				for (int k = checkBox[0]; k < checkBox[1]; k++){ //checks x variables on checkBox
    					for (int l = checkBox[2]; l < checkBox[3]; l++){ //checks y variables on checkBox
    						if (i == k && j == l){ //checks if values are the same (if there is a collision
    							return true; //returns true if there is a collision
    						}
    					}
    				}
    			}
    		}
     
    		return false; //returns false if there isn't a collision
    	}
     
     
    	public static int[][] getMap0(){
    		//map 0, aka level 1
    		//int[][] map = {{2,3, 0}, {2,2,2}, {3,3,3}}; a better way to do it. 
    		int[][] map = new int[32][12];  
    		map[0][0] = 1; map[1][0] = 0; map[2][0] = 0; map[3][0] = 0; map[4][0] = 0; map[5][0] = 0; map[6][0] = 0; map[7][0] = 0;
    		map[8][0] = 0; map[9][0] = 0; map[10][0] = 0; map[11][0] = 0; map[12][0] = 0; map[13][0] = 0; map[14][0] = 0; map[15][0] = 0; 
    		map[16][0] = 0; map[17][0] = 0; map[18][0] = 0; map[19][0] = 0; map[20][0] = 0; map[21][0] = 0; map[22][0] = 0; map[23][0] = 0;
    		map[24][0] = 0; map[25][0] = 0; map[26][0] = 0; map[27][0] = 0;	map[28][0] = 0; map[29][0] = 0; map[30][0] = 0; map[31][0] = 1;
     
    		map[0][1] = 1; map[1][1] = 0; map[2][1] = 0; map[3][1] = 0; map[4][1] = 0; map[5][1] = 0; map[6][1] = 0; map[7][1] = 0;
    		map[8][1] = 0; map[9][1] = 0; map[10][1] = 0; map[11][1] = 0; map[12][1] = 0; map[13][1] = 0; map[14][1] = 0; map[15][1] = 0; 
    		map[16][1] = 0; map[17][1] = 0; map[18][1] = 0; map[19][1] = 0; map[20][1] = 0; map[21][1] = 0; map[22][1] = 0; map[23][1] = 0;
    		map[24][1] = 0; map[25][1] = 0; map[26][1] = 0; map[27][1] = 0;	map[28][1] = 0; map[29][1] = 0; map[30][1] = 0; map[31][1] = 1;	
     
    		map[0][2] = 1; map[1][2] = 0; map[2][2] = 0; map[3][2] = 0; map[4][2] = 0; map[5][2] = 0; map[6][2] = 0; map[7][2] = 0;
    		map[8][2] = 0; map[9][2] = 0; map[10][2] = 0; map[11][2] = 0; map[12][2] = 0; map[13][2] = 0; map[14][2] = 0; map[15][2] = 0; 
    		map[16][2] = 0; map[17][2] = 0; map[18][2] = 0; map[19][2] = 0; map[20][2] = 0; map[21][2] = 0; map[22][2] = 0; map[23][2] = 0;
    		map[24][2] = 0; map[25][2] = 0; map[26][2] = 0; map[27][2] = 0;	map[28][2] = 0; map[29][2] = 0; map[30][2] = 0; map[31][2] = 1;	
     
    		map[0][3] = 1; map[1][3] = 0; map[2][3] = 0; map[3][3] = 0; map[4][3] = 0; map[5][3] = 0; map[6][3] = 0; map[7][3] = 0;
    		map[8][3] = 0; map[9][3] = 0; map[10][3] = 0; map[11][3] = 0; map[12][3] = 0; map[13][3] = 0; map[14][3] = 0; map[15][3] = 0; 
    		map[16][3] = 0; map[17][3] = 0; map[18][3] = 0; map[19][3] = 0; map[20][3] = 0; map[21][3] = 0; map[22][3] = 0; map[23][3] = 0;
    		map[24][3] = 0; map[25][3] = 0; map[26][3] = 0; map[27][3] = 0;	map[28][3] = 0; map[29][3] = 0; map[30][3] = 0; map[31][3] = 1;	
     
    		map[0][4] = 1; map[1][4] = 0; map[2][4] = 0; map[3][4] = 0; map[4][4] = 0; map[5][4] = 0; map[6][4] = 0; map[7][4] = 0;
    		map[8][4] = 0; map[9][4] = 0; map[10][4] = 0; map[11][4] = 0; map[12][4] = 0; map[13][4] = 0; map[14][4] = 0; map[15][4] = 0; 
    		map[16][4] = 0; map[17][4] = 0; map[18][4] = 0; map[19][4] = 0; map[20][4] = 0; map[21][4] = 0; map[22][4] = 0; map[23][4] = 0;
    		map[24][4] = 0; map[25][4] = 0; map[26][4] = 0; map[27][4] = 0;	map[28][4] = 0; map[29][4] = 0; map[30][4] = 0; map[31][4] = 1;	
     
    		map[0][5] = 1; map[1][5] = 0; map[2][5] = 0; map[3][5] = 0; map[4][5] = 0; map[5][5] = 0; map[6][5] = 0; map[7][5] = 0;
    		map[8][5] = 0; map[9][5] = 0; map[10][5] = 0; map[11][5] = 0; map[12][5] = 0; map[13][5] = 0; map[14][5] = 0; map[15][5] = 0; 
    		map[16][5] = 0; map[17][5] = 0; map[18][5] = 0; map[19][5] = 0; map[20][5] = 0; map[21][5] = 0; map[22][5] = 0; map[23][5] = 0;
    		map[24][5] = 0; map[25][5] = 0; map[26][5] = 0; map[27][5] = 0;	map[28][5] = 0; map[29][5] = 0; map[30][5] = 0; map[31][5] = 1;	
     
    		map[0][6] = 1; map[1][6] = 0; map[2][6] = 0; map[3][6] = 0; map[4][6] = 0; map[5][6] = 0; map[6][6] = 0; map[7][6] = 0;
    		map[8][6] = 0; map[9][6] = 0; map[10][6] = 0; map[11][6] = 0; map[12][6] = 0; map[13][6] = 0; map[14][6] = 0; map[15][6] = 0; 
    		map[16][6] = 0; map[17][6] = 0; map[18][6] = 0; map[19][6] = 0; map[20][6] = 0; map[21][6] = 0; map[22][6] = 0; map[23][6] = 0;
    		map[24][6] = 0; map[25][6] = 0; map[26][6] = 0; map[27][6] = 0;	map[28][6] = 0; map[29][6] = 0; map[30][6] = 0; map[31][6] = 1;	
     
    		map[0][7] = 1; map[1][7] = 0; map[2][7] = 0; map[3][7] = 0; map[4][7] = 0; map[5][7] = 0; map[6][7] = 0; map[7][7] = 0;
    		map[8][7] = 0; map[9][7] = 0; map[10][7] = 0; map[11][7] = 0; map[12][7] = 0; map[13][7] = 0; map[14][7] = 0; map[15][7] = 0; 
    		map[16][7] = 0; map[17][7] = 0; map[18][7] = 0; map[19][7] = 0; map[20][7] = 0; map[21][7] = 0; map[22][7] = 0; map[23][7] = 0;
    		map[24][7] = 0; map[25][7] = 0; map[26][7] = 0; map[27][7] = 0;	map[28][7] = 0; map[29][7] = 0; map[30][7] = 0; map[31][7] = 1;	
     
    		map[0][8] = 1; map[1][8] = 0; map[2][8] = 0; map[3][8] = 0; map[4][8] = 0; map[5][8] = 0; map[6][8] = 0; map[7][8] = 0;
    		map[8][8] = 0; map[9][8] = 0; map[10][8] = 0; map[11][8] = 0; map[12][8] = 0; map[13][8] = 0; map[14][8] = 0; map[15][8] = 0; 
    		map[16][8] = 0; map[17][8] = 0; map[18][8] = 0; map[19][8] = 0; map[20][8] = 0; map[21][8] = 0; map[22][8] = 0; map[23][8] = 0;
    		map[24][8] = 0; map[25][8] = 0; map[26][8] = 0; map[27][8] = 0;	map[28][8] = 0; map[29][8] = 0; map[30][8] = 0; map[31][8] = 1;	
     
    		map[0][9] = 1; map[1][9] = 0; map[2][9] = 0; map[3][9] = 0; map[4][9] = 0; map[5][9] = 0; map[6][9] = 0; map[7][9] = 0;
    		map[8][9] = 0; map[9][9] = 0; map[10][9] = 0; map[11][9] = 0; map[12][9] = 0; map[13][9] = 0; map[14][9] = 0; map[15][9] = 0; 
    		map[16][9] = 0; map[17][9] = 0; map[18][9] = 0; map[19][9] = 0; map[20][9] = 0; map[21][9] = 0; map[22][9] = 0; map[23][9] = 0;
    		map[24][9] = 0; map[25][9] = 0; map[26][9] = 0; map[27][9] = 0;	map[28][9] = 0; map[29][9] = 0; map[30][9] = 0; map[31][9] = 1;	
     
    		map[0][10] = 1; map[1][10] = 0; map[2][10] = 0; map[3][10] = 0; map[4][10] = 0; map[5][10] = 0; map[6][10] = 0; map[7][10] = 0;
    		map[8][10] = 0; map[9][10] = 0; map[10][10] = 0; map[11][10] = 0; map[12][10] = 0; map[13][10] = 0; map[14][10] = 0; map[15][10] = 0; 
    		map[16][10] = 0; map[17][10] = 0; map[18][10] = 0; map[19][10] = 0; map[20][10] = 0; map[21][10] = 0; map[22][10] = 0; map[23][10] = 0;
    		map[24][10] = 0; map[25][10] = 0; map[26][10] = 0; map[27][10] = 0;	map[28][10] = 0; map[29][10] = 0; map[30][10] = 0; map[31][10] = 1;	
     
    		map[0][11] = 1; map[1][11] = 1; map[2][11] = 1; map[3][11] = 1; map[4][11] = 1; map[5][11] = 1; map[6][11] = 1; map[7][11] = 1;
    		map[8][11] = 1; map[9][11] = 1; map[10][11] = 1; map[11][11] = 1; map[12][11] = 1; map[13][11] = 1; map[14][11] = 1; map[15][11] = 1; 
    		map[16][11] = 1; map[17][11] = 1; map[18][11] = 1; map[19][11] = 1; map[20][11] = 1; map[21][11] = 1; map[22][11] = 1; map[23][11] = 1;
    		map[24][11] = 1; map[25][11] = 1; map[26][11] = 1; map[27][11] = 1;	map[28][11] = 1; map[29][11] = 1; map[30][11] = 1; map[31][11] = 1;
     
    		return map;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Keyboard Listeners to Control object states

    Are you talking about a logic problem where the code is not correctly setting the state for when different keys are pressed?

    How have you tried debugging the code to see where it is going wrong? Try adding lots of printlns to print out the current state of the variables and the values of the variables as they are changed. The print out will help you understand what the code is doing and when.

    You'll need to provide a small simple program that compiles, executes and shows the problems. This code it too big and is missing too many class definitions for anyone to work with for testing.


    The code has too many static variables.
    Last edited by Norm; June 10th, 2012 at 07:31 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    DOLZero (June 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners to Control object states

    I was having problems getting the toggle breakpoint to start where I wanted the debugging to start. Not sure why, but when I clicked to add a breakpoint it never showed up. I will try adding in some println codes to see if that helps me figure out where I am going wrong. Thanks.

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Keyboard Listeners to Control object states

    This may solve your problem, or it may do nothing, but it is HIGHLY advised to put break statements at the end of each case in a switch statement. For example, I took a bit of your code and edited it to include break statements:
    switch (e.getKeyCode()){
    case KeyEvent.VK_LEFT:
    	player.moveLeft = false;
    	break;
    case KeyEvent.VK_RIGHT:
    	player.moveRight = false;
     	break;
    case KeyEvent.VK_SPACE:
    	player.setJump(false);
    	break;
    }

    Like I said, making this change may solve all your problems or have no effect what-so-ever, but it is something that is reasonably important to keep in mind.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners to Control object states

    I had thought about that, but the main reason I didn't want to include a break for those was because I wanted players to be able to press multiple buttons at once. If someone is holding down up and right arrows, I would like to have the player sprite move both up and to the right. Putting a break in there would probably activate the first direction in the code, and then ignore anything else being pressed.

    At least that is what I would expect to happen. I will give it a try though.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Keyboard Listeners to Control object states

    Putting a break in there would probably activate the first direction in the code, and then ignore anything else being pressed.
    Not at all. The missing breaks mean that all the cases following the first one that matched would be executed, no matter what case they were testing for. The break prevents that.

    To test for multiple pressed buttons, you should use a list of if statements each of which tests for a button pressed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners to Control object states

    Sounds like a plan. I am gonna rewrite much of my code and try to clean up all of that mess I have going on. My system for collision detection is far from efficient, and really bogs down the game loop. I have been reading up on a few different ways of working that. My guess is that it's likely I will have some new questions once I get to that point. Thanks again for the assistance.

Similar Threads

  1. [SOLVED] Keyboard Listeners and ImagePanel
    By DOLZero in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 15th, 2012, 07:26 AM
  2. Listeners not working!
    By Alex-Green in forum Member Introductions
    Replies: 1
    Last Post: February 19th, 2012, 07:49 PM
  3. Mouse Listeners
    By newbie in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2010, 11:08 PM
  4. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 09:27 AM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 07:10 AM