Results 1 to 4 of 4
- 02-04-2011, 04:03 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
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; } } } }
- 02-04-2011, 04:51 PM #2
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!
- 02-04-2011, 04:52 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
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! ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-04-2011, 05:20 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
mp5 left!!.. anyOne know it?.
By jhon123 in forum New To JavaReplies: 0Last Post: 01-31-2011, 05:50 AM -
jasperreport-right to left
By marjanzfz in forum Advanced JavaReplies: 1Last Post: 09-15-2009, 03:06 PM -
Align left
By britto_bicsjohn in forum AWT / SwingReplies: 2Last Post: 09-09-2009, 04:05 AM -
Any android developers left?
By cbyte in forum Jobs OfferedReplies: 0Last Post: 09-25-2008, 10:02 AM -
menu in top left always
By sschwinghammer in forum New To JavaReplies: 2Last Post: 02-06-2008, 02:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks