Results 1 to 2 of 2
- 10-12-2011, 03:28 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Simple Arrow Key Movement Troubles
Hi guys,
I am having some trouble making a very simple program using arrow-key movement. The program is supposed to simply move an image according to what arrow key is pressed. I originally did this without using while loops or booleans (and it worked) but the animation was extremely jumpy. I was told that the easiest way to fix this would be to use boolean statements that toggle when the key is pressed/released, thereby activating/deactivating while loops that control movement. The following code is called from a separate initiation class, but the real meat and bones of the movement are in here. When I run it, I receive no error, but the character refuses to move when i press the arrow keys. The window also does not close, making me think the program is stuck in one of the while loops. Help please?
Java Code:package Enemies; import java.awt.*; import java.awt.event.*; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JFrame; import java.io.IOException; import java.awt.Rectangle; public class Character extends Canvas { // Read about JFrames and Canvas objects // http://en.wikibooks.org/wiki/Java_Programming/Canvas // Global variable boolean upmove = false; boolean downmove = false; boolean rightmove = false; boolean leftmove = false; int myX; int myY; public Character(int x, int y,JFrame window) { setSize(new Dimension(window.getWidth(), window.getHeight())); // set size of Canvas myX = x; myY = y; // set globals from calling program // IMPORTANT - this is an object that listens for user key input addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { moveTrue(evt); // custom method to evaluate user input moveChar(); } public void keyReleased(KeyEvent evt) { moveFalse(evt); moveChar(); } }); } // This method is a built in method of a canvas that can be overridden to draw what you want public void paint(Graphics g) { try { Image link = new DrawImage().LoadImage("link.png"); g.drawImage(link,myX,myY,null); g.setColor(Color.red); Rectangle playerBox = new Rectangle(myX+1,myY+1,1,1 ); //Rectangle boxDet = new Rectangle(0,0,50,50); //if (boxDet.intersects(playerBox)) { // g.setColor(Color.red); // g.drawString("You made it!",0,10); //} // g.fillOval(myX, myY, 30, 30); } catch (IOException e) { System.out.println("ARE YOU DUMB?????"); } } public void moveTrue(KeyEvent evt) { switch (evt.getKeyCode()) { case KeyEvent.VK_DOWN: downmove = true; // this is the section that //myY += 5; //break; case KeyEvent.VK_UP: upmove = true; // myY -= 5; // break; case KeyEvent.VK_LEFT: leftmove = true; // myX -= 5; // break; case KeyEvent.VK_RIGHT: rightmove = true; // myX += 5; // break; } // repaint(); // This built in method calls the canvas paint method } public void moveFalse(KeyEvent evt) { switch (evt.getKeyCode()) { case KeyEvent.VK_DOWN: downmove = false; //myY += 5; //break; case KeyEvent.VK_UP: upmove = false; // myY -= 5; // break; case KeyEvent.VK_LEFT: leftmove = false; // myX -= 5; // break; case KeyEvent.VK_RIGHT: rightmove = false; // myX += 5; // break; } // repaint(); // This built in method calls the canvas paint method } public void moveChar() { while (downmove == true) { myY += 5; } while (upmove == true) { myY -= 5; } while (leftmove == true){ myX -= 5; } while (rightmove == true){ myX += 5; } repaint(); } }
- 10-12-2011, 05:44 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Simple Arrow Key Movement Troubles
Well that is easy to check. Did you add a System.out.println(...) to check your theory?The window also does not close, making me think the program is stuck in one of the while loops.
Also, don't use a Canvas. This is a Swing application so your should be using a JPanel (or JComponent) for a custom component.
Similar Threads
-
Arrow Keys?
By Alerhau in forum New To JavaReplies: 10Last Post: 06-17-2011, 07:45 PM -
Help in bow and arrow game
By sahildave1991 in forum AWT / SwingReplies: 6Last Post: 10-18-2010, 02:34 PM -
Arrow Button Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
Draw an arrow
By Albert in forum SWT / JFaceReplies: 3Last Post: 02-01-2008, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks