Results 1 to 5 of 5
Thread: How to use the sleep and thread?
- 08-06-2007, 06:31 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 3
- Rep Power
- 0
How to use the sleep and thread?
I'm coding a program to move a player on the screen,
but the program directly run to the end,
the animation part dose not display,
how can i modify my code to show the movement of the player?
here is the run function,
Java Code:Thread t, w; public void run() { while (isClick) { try { repaint(); t.sleep(500); int j = playerStep + 40; while(playerStep < j) { try { playerStep += 10; repaint(); w.sleep(300); } catch (InterruptedException err) { JOptionPane.showMessageDialog(null, "Moving error!"); } } } catch (InterruptedException err) { JOptionPane.showMessageDialog(null, "Drawing error!"); } isClick = false; repaint(); } }
Thanks a lot.
- 08-06-2007, 07:02 PM #2levent Guest
You set isClick to false at the end of your loop. That makes your loop to run only once. This is why you see no movement!
- 08-06-2007, 07:18 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 3
- Rep Power
- 0
- 08-06-2007, 07:24 PM #4levent Guest
If so just remove the sleep statements and your loop and do only the following:sorry that, i did not mention, i use a button click to control the movement.
But if the button is only starting the movement (which means you will not need to press the button on each movement), then you still should not have "isClick = false;" there. You can place it inside an if statement in that case:Java Code:playerStep += 10; repaint();
Java Code:if (playerStep == 100) isClick = false;
- 08-07-2007, 02:56 AM #5
Member
- Join Date
- Aug 2007
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
How to use sleep method of the Thread class
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:42 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks