• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep inside a loop

 
bab javier
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to move an image in slow motion. problem is putting sleep() inside a loop doesn't give that effect. instead, it runs the loop(behind the scenes) and will just show u the finished output. for example:
i have to get point A to point B
- effect should be A................B (dots indicating slow motion)
but i get
- B
in J2SE, sleep() works fine inside a loop. can't figure out why it isn't in J2ME.
pls enlighten me guys
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sleep works fine in a loop in J2ME too. Can you show us a bit more of your code?
Is the thread you're calling sleep on the main MIDP UI thread (i.e. the thread that calls your canvas's 'paint' and 'commandAction' methods?) If so, try having a separate animation thread - you should never do anything that takes a long time in the main MIDP UI thread. If you already have a separate animation thread, are you calling 'serviceRepaints' each time after you call 'repaint'? Check out the animation loop in Forum Nokia's example MIDlet 'Boids' - it should make this clear for you.
 
bab javier
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a piece of the code....
public void paint(Graphics g){
g.setColor(white);
g.fillRect(wholeScreen);
player.draw(g); // player is a sprite object
}
public void move(int keyCode){
if(keyCode == Canvas.KEY_NUM1
jump();
}
void jump(){ // moves up and then down
int count = 0;
while(count != 48){
try{
Thread.sleep(1000);
}catch(InterruptedException i){}

player.move(-1,-1); //changes the x and y coordinates
count++;
repaint();
}
if(count == 48){
while(count != 0){
player.move(-1,1);
count--;
repaint();
}
}
}
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you're calling your 'move' method from your 'keyPressed' method - is that right? You'll get the effect you're looking for by calling 'serviceRepaints()' immediately after each time you call 'repaint()'.
The reason: the thread that calls 'keyPressed' is the same thread that will call 'paint' later when the JVM notices that you've called 'repaint', but it won't get a chance until 'keyPressed' returns. Calling 'repaint' really just tells the JVM that the screen is out of date and needs repainting soon - the JVM won't call 'paint' straight away. Calling 'serviceRepaints' tells the JVM to handle all the outstanding repaint requests (they'll probably be merged into one) by calling 'paint' before continuing.
But you're still doing the animation wrong, I'm afraid. Please download that Boids example from Forum Nokia (you get source code and a PDF tutorial) and have a look at the 'run' method of class BoidsCanvas. You'll see how to do the animation in a separate thread, which is what you must do in MIDP 1.0.
 
bab javier
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again david! i'll look into the boids example
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic