Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Strange Sprite Animation  (Read 892 times)
0 Members and 1 Guest are viewing this topic.
Offline javamobilegame

Senior Newbie




Java games rock!


« Posted 2005-03-10 13:33:38 »

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? Huh  Thanks for your reply!
Offline wooD

Senior Newbie




J2ME Developer


« Reply #1 - Posted 2005-03-10 20:54:36 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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) {}
  }
}

Offline javamobilegame

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-03-11 16:11:47 »

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~
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline wooD

Senior Newbie




J2ME Developer


« Reply #3 - Posted 2005-03-15 20:47:44 »

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

Offline javamobilegame

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-03-17 16:10:08 »

I see. Thanks for your explaination~ Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-17 21:29:12

alaslipknot (75 views)
2013-05-16 21:24:48

gouessej (106 views)
2013-05-16 00:53:38

gouessej (103 views)
2013-05-16 00:17:58

theagentd (114 views)
2013-05-15 15:01:13

theagentd (103 views)
2013-05-15 15:00:54

StreetDoggy (148 views)
2013-05-14 15:56:26

kutucuk (171 views)
2013-05-12 17:10:36

kutucuk (169 views)
2013-05-12 15:36:09

UnluckyDevil (178 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.133 seconds with 21 queries.