Page 1 of 1

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

#1 fooboo  Icon User is offline

  • The king of Nynex
  • member icon

Reputation: 4
  • View blog
  • Posts: 3,107
  • Joined: 28-July 06

Posted 17 November 2006 - 04:03 AM

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:

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:

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:

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:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Quick tip on timing in J2ME

#2 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

Posted 13 July 2011 - 02:12 PM

Nice post. Now using time in a time related game, say you have 60 seconds to complete the game, and it runs on the screen, would you have the time running in a concurrent thread of its own, or just keep checking at the end of the loop. which could be a couple milliseconds late due to all the code in the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1