You Are Here:

Community: Developer Discussion Boards

#1 Old usage of timer - 2003-12-01, 08:23

Join Date: Nov 2003
Posts: 3
dzoy
Offline
Registered User
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!
Reply With Quote

#2 Old 2003-12-01, 12:07

Join Date: Mar 2003
Posts: 15
Location: Finland
sampoy
Offline
Registered User
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.
Reply With Quote

#3 Old 2003-12-02, 04:44

Join Date: Nov 2003
Posts: 3
dzoy
Offline
Registered User
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.
Reply With Quote

#4 Old 2003-12-02, 08:21

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
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 With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia