Recently completed a J2ME game, always want to write something to everyone ~ because before always in the request, now put J2ME knowledge-timer knowledge and I have a little experience-pay to everyone, content is not much but its use, I hope that the introduction of the J2ME developers help.
First of all, the basic use of the timer, the following excerpt from the Nokia Forum in an article:
Design Java applications for the 60 series platform-timer
The various timers allow a variety of applications to easily plan tasks without having to allocate threads. Using threads often requires more complex designs and requires more system resources, which are more luxurious for a variety of gadgets.
Create an example of a timer:
public class MyTask extends TimerTask
{
private int iCount;
public MyTask(int aStart)
{
super();
iCount = aStart;
}
public void run()
{
iCounter--;
System.out.println(“Counter is now ” + iCounter);
if (iCounter == 0)
cancel();
}
}
Build a timer and add MyTask to it:
MyTask myTask = new Mytask(50);
Timer myTimer = Timer();
MyTimer.schedule(myTask, 1000, 20000);
In this example, the MyTask run () method is invoked every 20 seconds.
does not guarantee that the timer task must be performed on time. Each timer task occurs sequentially. If one of the tasks takes a long time, the next task can only be executed after the current task completes. For a recurring task, it is best to ensure that the run () method is completed quickly.
More than one timer is possible, so you can assign tasks to several timers. However, use multiple timers with caution, because each timer only runs its own thread, and synchronization may be required.
The above is the basic use of the timer, for some simple timer operation to do so, directly to execute the code into the run. For a complex logical structure, you need to do something else when you want to execute the Run method multiple times. Here is a frame I use the timer, in fact, is an object-oriented application, relatively simple to understand, I hope to have a help, but also want to have different views of friends to communicate together.
Also have a class to inherit TimerTask:
import java.util.TimerTask;
public class GAMETask extends TimerTask {
private GameObject m_Object;
public GAMETask(GameObject object) {
m_Object = object;
}
public void run() {
m_Object.gameTask();
}
}
Then define a Gameobject interface:
public interface GameObject {
public abstract void gameTask();
}