Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2007, 07:31 PM
Member
 
Join Date: Aug 2007
Posts: 3
jiuhu is on a distinguished road
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,


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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 08:02 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-06-2007, 08:18 PM
Member
 
Join Date: Aug 2007
Posts: 3
jiuhu is on a distinguished road
Quote:
Originally Posted by levent View Post
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!

sorry that, i did not mention, i use a button click to control the movement.
whichmeans, whenever i click on the button, it will move.

by the way, this code can run, but it directly show me the final screen, which is i can not see the progress of the movement.

thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-06-2007, 08:24 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
sorry that, i did not mention, i use a button click to control the movement.
If so just remove the sleep statements and your loop and do only the following:

Code:
playerStep += 10; repaint();
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:

Code:
if (playerStep == 100) isClick = false;
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-07-2007, 03:56 AM
Member
 
Join Date: Aug 2007
Posts: 3
jiuhu is on a distinguished road
thanks levent.

but i think this din't solve my problem,
as my player still direct jump to the end point
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-23-2008 11:30 PM
How to use sleep method of the Thread class Java Tip java.lang 0 04-09-2008 07:42 PM
Can't get my thread to sleep! jamesfrize New To Java 2 03-25-2008 06:14 AM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 08:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 10:31 AM


All times are GMT +3. The time now is 12:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org