Results 1 to 4 of 4
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Thumbs down square moves left and down but not up or left

    when you run this you should be able to press the right and down arrow keys which move the square, but up and left keys do not move the square as it should .. do you know why this is?

    Java Code:
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    public class square{
    	int x;
    	int y;
    	int movespeed = 4;
    
    	public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	new square();
                }
            });
    	}
    	public square(){
    		JFrame panel = new JFrame();
    		panel.setExtendedState(Frame.MAXIMIZED_BOTH);
    		panel.setSize(600, 400);
    		panel.setVisible(true);
    		panel.getContentPane().add(new painting());
    		panel.requestFocusInWindow();
    		panel.addKeyListener(new moving());
    	}
    	@SuppressWarnings("serial")
    	public class painting extends JComponent{
    	    public void paint(Graphics g){
    	      g.setColor(Color.red);
    	      g.fillRect(x,y,50,50);
    	      repaint();
    	    }
    	  }
    
    	public class moving extends KeyAdapter implements ActionListener{
    		boolean up = false;
    		boolean down = false;
    		boolean left = false;
    		boolean right = false;
    		public moving(){
    			Timer timer = new Timer(55, this);
    			timer.start();
    		}
    		@Override
    		public void keyPressed(KeyEvent arg0) {
    			 switch(arg0.getKeyCode())
    			  {
    			     case KeyEvent.VK_LEFT: left = true;
    			     case KeyEvent.VK_RIGHT: right = true;
    			     case KeyEvent.VK_UP: up = true;
    			     case KeyEvent.VK_DOWN: down = true;
    			  }
    }
    
    		@Override
    		public void keyReleased(KeyEvent arg0) {
    			  switch(arg0.getKeyCode())
    			  {
    			     case KeyEvent.VK_LEFT: left = false;
    			     case KeyEvent.VK_RIGHT: right = false;
    			     case KeyEvent.VK_UP: up = false;
    			     case KeyEvent.VK_DOWN: down = false;
    			  }
    		}
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    		public void actionPerformed(ActionEvent ac) {
    				  if(left && down){
    				     x = x - movespeed;
    				     y = y + movespeed;
    				  }
    				  if(left && up){
    					 x = x - movespeed;
    				     y = y - movespeed;
    				  }
    				  if(left){
    					 x = x - movespeed;
    				  }
    				  if(right && down){
    					 x = x + movespeed;
    				     y = y + movespeed;
    				  }
    				  if(right && up){
    					 x = x + movespeed;
    				     y = y - movespeed;
    				  }
    				  if(right){
    					 x = x + movespeed;
    				  }
    				  if(down){
    				     y = y + movespeed;
    				  }
    				  if(up){
    				     y = y - movespeed;
    				  }
    			}
    		}
    		
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    You need to break out of each case in your switch statement, otherwise it will fall through and do anything below it.

    Recommended reading: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by natdizzle View Post
    switch(arg0.getKeyCode())
    {
    case KeyEvent.VK_LEFT: left = true;
    case KeyEvent.VK_RIGHT: right = true;
    case KeyEvent.VK_UP: up = true;
    case KeyEvent.VK_DOWN: down = true;
    }
    You should put a couple of 'break' statements there, otherwise the control flow 'falls through', e.g. if the key code equals VK_UP both up and down are set to the value true.

    kind regards,

    Jos

    edit: @KevinWorkman: don't do that! It's driving me crazy to be the slowest old sod in town! ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

Similar Threads

  1. mp5 left!!.. anyOne know it?.
    By jhon123 in forum New To Java
    Replies: 0
    Last Post: 01-31-2011, 05:50 AM
  2. jasperreport-right to left
    By marjanzfz in forum Advanced Java
    Replies: 1
    Last Post: 09-15-2009, 03:06 PM
  3. Align left
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 04:05 AM
  4. Any android developers left?
    By cbyte in forum Jobs Offered
    Replies: 0
    Last Post: 09-25-2008, 10:02 AM
  5. menu in top left always
    By sschwinghammer in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 02:39 PM

Posting Permissions

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