Results 1 to 3 of 3
Thread: Moving Box
- 08-30-2009, 01:58 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
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 :)
- 08-30-2009, 04:24 AM #2
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
- 08-30-2009, 12:29 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
randomly moving shapes
By marlon in forum New To JavaReplies: 3Last Post: 10-05-2012, 09:37 AM -
Need Help with Java 2d - moving train
By rtm09 in forum New To JavaReplies: 7Last Post: 04-15-2009, 12:28 AM -
Moving textboxes
By GabWit in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:07 PM -
moving a file
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 07:52 PM -
examples of moving objects
By fred in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks