Results 1 to 2 of 2
  1. #1
    javathon is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default 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();
        	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Simple Arrow Key Movement Troubles

    The window also does not close, making me think the program is stuck in one of the while loops.
    Well that is easy to check. Did you add a System.out.println(...) to check your theory?

    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

  1. Arrow Keys?
    By Alerhau in forum New To Java
    Replies: 10
    Last Post: 06-17-2011, 07:45 PM
  2. Help in bow and arrow game
    By sahildave1991 in forum AWT / Swing
    Replies: 6
    Last Post: 10-18-2010, 02:34 PM
  3. Arrow Button Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  4. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 08:11 AM

Posting Permissions

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