It's standard practice to nail your frame rate to a fixed number and 30fps is what I usually use. Here is some code I use to run my game simulation in a fixed time step.

public final void run()
{
    System.out.println( "UpdateThread: start" );

    while( m_bKill == false )
    {
        synchronized( this )
        {
            if ( m_bPaused == true )
            {
                try
                {
                    wait( 1000 );
                }

                catch( InterruptedException x ) { }
            }
        }

        if ( m_bPaused == false )
        {
            long    currentTime = System.currentTimeMillis();
            long    elapsed        = currentTime - m_LastTickTime;
            if ( elapsed>= m_TickLengthMS )
            {
                onUpdate();

                m_LastTickTime += m_TickLengthMS;
            }
            else
            {
                Thread.yield();
            }
        }
    }

    System.out.println( "UpdateThread: killed" );
}


In reality there can be any number of things causing this frame rate problem. Your simulation could be spiking due to collision, AI or any number of things. Also if your simulation physics is running off your frame rate then this will be a problem. Mind you if you ALWAYS can make >30fps it won't be a problem.

Leigh


On 7/22/2010 4:16 PM, Cameron wrote:
Thanks for the reply Matt,

I have used the DDMS in Eclipse to see if there is any GC that happens
and nothing that belongs to my thread. There are some but those are by
the Android system itself.

I have also optimized the hell out of this game following all of the
examples from google and other forums.

I do exaclty what you said with the adding new monsters example on the
go but it doesn't seem to great any GC events. I will preload them
like you suggested and see what happens.

Also I will try to use the uptimeMillis(). Right now im using "now =
System.currentTimeMillis();"

On Jul 22, 1:07 pm, Matt<[email protected]>  wrote:
Well, it sounds like the frame rate goes down intermittently because
of the garbage collector.

First of all, you should not be doing any object creation during your
game loop.  This way there is never any need for a GC.
1. Create all of your objects before hand.  For example,
Monster[] mMonsters = new Monster[Monster.MAX];
setup() {
  for (int i = 0; i<  mMonsters.length; i++) {
   mMonsters[i] = new Monster();
   mMonsters[i].used = false;
  }
  ...}

Instead of creating a new monster every time one comes into play, just
set its used flag to true (and set it to false, instead of deleting
it, when the monster is done).

2. Use android.os.SystemClock.uptimeMillis() when limiting your FPS.
Basically what you want to do is keep track of each game tick, and if
it hasn't been 1/30 seconds yet, sleep for the remaining time.

3. Google "java game optimization".

-Matt

On Jul 22, 3:46 pm, Cameron<[email protected]>  wrote:



I based my game off of the lunar lander demo, although heavily
modified, and I can get around 40-50fps but the problem is it
fluctuates between 40-50fps so much that it causes the moving graphics
to jitter! Its very annoying and makes my game look really shitty when
in fact its running at a good frame rate.
I tried setting the thread priority higher but that just made it
worse... now it will fluctuate between 40-60fps...
I was thinking of limiting the FPS to about 30 so that it will be
constant. Is this a good idea and does anyone else have experience or
a different solution?
Thanks!
PS: If you have an example of limiting or creating constant FPS that
would be helpful. I've tried it before but never got it quite right.

--
Leigh McRae
www.lonedwarfgames.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to