Results 1 to 3 of 3

Thread: Moving Box

  1. #1
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default Moving Box

    Hiya, I wrote a program that you move a box around using arrow keys. I figured how to move the box according to two keys pressed simultaneously like right and up. Here's the code:

    Java Code:
    public void keyReleased(KeyEvent evt){
    		
    		int rel = evt.getKeyCode();
    
    		if(rel == KeyEvent.VK_UP) { 
    			isUp = false;
    		}
    		if(rel == KeyEvent.VK_DOWN) {
    			isDown = false;
    		}
    			
    		if(rel == KeyEvent.VK_LEFT) {
    			isLeft = false;
    		} 
    		if(rel == KeyEvent.VK_RIGHT) {
    			isRight = false;
    		}
    		
    	}
    Java Code:
    	public void keyPressed(KeyEvent evt) {
    		
    		key = evt.getKeyCode();
    
    
    		if(key == KeyEvent.VK_LEFT) {
    			
    			if(isUp) { 
    				squareY -= 5;
    				if(squareY<3) squareY = 3;
    			}
    			
    			if(isDown) {
    				
    				squareY += 5;
    				if(squareY>getHeight()-43) squareY = getHeight() -43;
    				
    			}
    			
    			squareX -= 5;
    			if(squareX<3) squareX = 3;
    			isLeft = true;
    			repaint();
    		}
    		
    		if(key == KeyEvent.VK_RIGHT) {
    			
    			if(isUp) { 
    				squareY -= 5;
    				if(squareY<3) squareY = 3;
    			}
    			
    			if(isDown) {
    				
    				squareY += 5;
    				if(squareY>getHeight()-43) squareY = getHeight() -43;		
    			}
    			
    			squareX += 5;
    			if(squareX>getWidth()-43) squareX = getWidth()-43;
    			isRight = true;
    			repaint();
    		}
    		
    		if(key == KeyEvent.VK_UP) {
    			
    			if(isRight) {
    				squareX += 5;
    				if(squareX>getWidth()-43) squareX = getWidth()-43;
    			}
    			
    			if(isLeft) {
    				squareX -= 5;
    				if(squareX<3) squareX = 3;
    			}
    			
    			
    			squareY -= 5;
    			if(squareY<3) squareY = 3;
    			isUp = true;
    			repaint();
    		}
    		
    		if(key == KeyEvent.VK_DOWN) {
    			
    			if(isRight) {
    				squareX += 5;
    				if(squareX>getWidth()-43) squareX = getWidth()-43;
    			}
    			
    			if(isLeft) {
    				squareX -= 5;
    				if(squareX<3) squareX = 3;
    			}
    			
    			squareY += 5;
    			if(squareY>getHeight()-43) squareY = getHeight() -43;
    			isDown = true;
    			repaint();
    		}
    	}

    The problem is, for example: when I press UP + RIGHT, it goes that way ok cool, but when i release RIGHT, the box stops. I want it to keep going (to up in this case). Thank you :)

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    instead of handling the update position state on keyboard event, you could just record the event and have a seperate update thread that does the processing.

    In other words, when keyboard happens then set a flag for up/down and left/right and have a thread that checks ever 50ms or so to update the position.

    Java Code:
    //inner class
    private classs updateThread extends Thread
    {
       public void run()
       {
          try
          {
              sleep(50); //sleep 50 ms
              ...
              //update pos on flags 
          }
          catch(Exception e) { ... }
       }
    }
    My Hobby Project: LegacyClone

  3. #3
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    I'm not familiar with threads, but I'll check some tutorials for it. Thank you.

Similar Threads

  1. randomly moving shapes
    By marlon in forum New To Java
    Replies: 3
    Last Post: 10-05-2012, 09:37 AM
  2. Need Help with Java 2d - moving train
    By rtm09 in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 12:28 AM
  3. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 04:07 PM
  4. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:52 PM
  5. examples of moving objects
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:06 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
  •