| Reply | « Previous Thread | Next Thread » |
|
hi! I'm curretly having a hard time implementing a timer in our game. The idea behind the game is that the power ups will be running just in 10 sec.. how can I do it. I have checked some timer implementation but I can't understand it. please help me... thanks!
|
|
Hi,
Create a class that extends TimerTask. overwrite the run() method to do something that is to be done in f.e. every 10 seconds. pseudoexample : public class GameTimer extends TimerTask { public void run() { game.doSomething(); } } In your game, create a timer & schedule the timertask to run every 10 seconds : Timer t = new Timer(); t.schedule(new GameTimer(),0,10000); second parameter is how long it takes before the task is run first time, and third is the interval between run() methodcalls, ms. |
|
if i'm going use the TimeTask i still need to create new class which will consume another heap memory.. is there other ways that I would not need to create new class? Thanks for your reply I do appreciate it.
|
|
There are couple of other ways:
1. Use Timer thread just as you would do in desktop Java. This will also eat memory and creating extra thread is not generally recommended in J2ME 2. If you have at least one class that is inherited from Object you can make it inherit from TimerTask and force it act as its own role plus TimerTask ;) This will save some heap space that would be used for class loading, but those TimerTasks get created and destroyed quite often, you need some small class to act as a TimerTask |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|