Results 1 to 5 of 5
  1. #1
    jiuhu is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    levent Guest

    Default

    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!

  3. #3
    jiuhu is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    levent Guest

    Default

    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:

    Java 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:

    Java Code:
    if (playerStep == 100) isClick = false;

  5. #5
    jiuhu is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

    Default

    thanks levent.

    but i think this din't solve my problem,
    as my player still direct jump to the end point

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  2. How to use sleep method of the Thread class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:42 PM
  3. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 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
  •