Strange Sprite Animation

I use a random integer to determine which image will be
fallen (i.e: just like Tetris), so in the run method I apply:
public void run()
{
long st=0;
long et =0;
while(conti) {
st = System.currentTimeMillis();
y = y + 10;
repaint();
doDraw3(g);
randInt = Math.abs(rand.nextInt()%2);
System.out.println("name ID = "+nameID);
et = System.currentTimeMillis();
if ((et-st)<rate)
{
try { Thread.sleep(rate-(et-st));} catch (Exception exp) {}
}
}
}

For the paint method i.e doDraw3(g):
public void doDraw3(Graphics g) {
g.setColor(0x00000000);
g.fillRect(0, 0, 300, 300);

try {img00 = Image.createImage("/00.png");
img01 = Image.createImage("/01.png");
}catch(Exception e) {}
if(randInt==0) {
nameID = “0”;
s0=createNumber("/00.png");
}
else if(randInt==1) {
nameID = “1”;
s1=createNumber("/01.png");
}
}
where s0 and s1 are sprites.
The result is the image is fallen from the top to the bottom, but the image is changing for every MillisSecond during the falling process.
I want to fix them. Do I have something is go wrong? ??? Thanks for your reply!

You are changing randInt every tick and using that to choose which image to display.

You need to separate randInt from the current Y location and only change the randInt and the beginning of the process when the image reaches the bottom and starts again from the top.


public void run()
{  
  long st=0;
  long et =0;  
  while(conti) 
  {
  st = System.currentTimeMillis();

  if (y==bottom)
    {
    // the current falling image has reached the bottom
    // choose a new image and reset the image location
    // to the top
    randInt = Math.abs(rand.nextInt()%2);
    y = top;
    }
  else
    {
    y = y + 10;
    }
  repaint();
  doDraw3(g);
  System.out.println("name ID = "+nameID);
  et = System.currentTimeMillis();
  if ((et-st)<rate)
  {
  try { Thread.sleep(rate-(et-st));} catch (Exception exp) {}
  }
} 

Thanks for your reply, Mr Wood!
And my second question is does it necessary to add “doDraw3(g)” in the while loop?
since I already apply “y=y+10” and “repaint()” but when I remove the code of “doDraw3(g)”, the “repaint” does not work anything for “y=y+10” for me (i.e the screen remains unchanged).
Thanks for your reply~

Looks like I missed that as well.

The doDraw3(g) should be in your paint method. If this is using the 3d apis, it may be different. I’ve not used them, so someone else will have to chime in. If it’s just a regular midlet, then put the doDraw3 in the paint method.

You may want to call serviceRepaints() after the repaint() method. This should (it has problems on some phones) wait at that point until the screen gets refreshed. I use it in all my games and have used wait()/notify() on phones that it doesn’t work on.

Wood

I see. Thanks for your explaination~ :smiley: