Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

make man jump in a game

public void jump(){
    int speed=-30;
    for(int i=0;i<60;i++){
    p.setY(speed);
    speed+=1;
    repaint();
    Thread.sleep(20);
    }
}

when i put this method no the constructor the man animates fine
but when i put a keyListener and press the up button the man goes to the desired location but i cant see him while hes going

0
omashhour
Asked:
omashhour
  • 2
1 Solution
 
CEHJCommented:
You need to increment the x,y coordinates on the timer until it arrives at the new location - not so much 'jump' as 'move'
0
 
GrandSchtroumpfCommented:
You should do your jump in a separate thread:

 public void jump() {
    Thread jumpThread = new Thread() {
      public void run() {
        try {
          int speed=-30;
          for(int i=0;i<60;i++){
            p.setY(speed);
            speed+=1;
            repaint();
            sleep(20);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    };
    jumpThread.start();
}
0
 
GrandSchtroumpfCommented:
:°)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now