Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 108,965 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,377 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Quick tip on timing in J2ME

 
Reply to this topicStart new topic

> Quick tip on timing in J2ME, Basically don't just increment and int

Rating  5
fooboo
Group Icon



post 17 Nov, 2006 - 04:03 AM
Post #1


This is just a quicky for any newbie reading this that is writing J2ME code that they intend to port to other phones.

I use states in my games so if you call a method by setting the game to the next state it'll loop until the state is changed again.

A common habit that you soon learn to drop is using an integer in a loop to time screen transitions, connections, game features etc.

If you put somewhere in your method an integer increment then rely on the methods looping to increment like:

CODE

i++;

if(i>100)
{
  state=nextState;
}


you will get a pause on older phones then it will move on. But if you try to port this code to a newer faster phone the pause will be significantly smaller and on some phones immeasurable.

You can see why if you look at your output window when running the code in an emulator. The faster the processor the faster it counts.

You could go through your ports making this number bigger and bigger per phone but there is a better more portable way.

(You may think this sounds daft but I have downloaded sample games from websites that have used this method and the game/app zips past at an unplayable/unusable speed.)

The sensible way of timing something, so that it takes exactly the same amount of time on any phone no matter how fast the processor runs, is to use a standard unit of time i.e. the millisecond.
Milliseconds are the same length on any phone.

What you do is take two snapshots of the current time in milliseconds and compare them. The difference will be a given number of milliseconds and you can decide how many of these you want to pass before your game moves on.

I use this method all over but one common one is how long I want the company logo to show on the screen before the game starts. The logo is drawn by a method that keeps looping until the right number of milliseconds is reached.

So before you enter the method you want to time you take a snapshot of the current time:

CODE

gameTime1=(int)System.currentTimeMillis();


Then in the method you take another snapshot subtract the first and then compare that number to the number of milliseconds you want to pass:

CODE

gameTime2=(int)System.currentTimeMillis()-gameTime1;

if(gameTime2>3000)
{
  state=nextState;
}


This example will wait 3 seconds before moving in to the next state so if for example you were drawing a logo you would need something like:

CODE

private void drawLogo(Graphics g)
{
  int gameTime2;
  g.drawImage(logoImage,0,0,g.TOP|g.LEFT);
  gameTime2=(int)System.currentTimeMillis()-gameTime1;
  if(gameTime2>3000)
  {
    state=nextState;
  }
}


The difference between the number in this example and the number in the increment example is that the first number is effectively a number of loops no matter how long that may take. This number is an exact number of milliseconds. Raising and lowering it will change how long it will wait but whatever you set it to will be the same amount of time for every port. No need to change it as you move on to other phones.

This tip uses states to control game flow. If anyone is interested I may write a tip about using states too.

I write in J2ME for phones but this method will work elsewhere too.
Wherever you use code on more than just your own machine you will get differences in speed so timing instead of increments is always a good idea.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/4/08 10:08PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month