hello. im currently developing an rpg. just want to ask if its ok to use a timer to control my game loop rather than thread. my application is doing well with this timer but it hangs sometimes. i wonder if the timer has something to do with this. ??? i tried to convert the timer into thread but my application doesn’t work the way i wanted. please help. thanks :’(
What do you mean by ‘doesn’t run the same way’? I’m not sure of the exact performance comparison, but I would have thought that the thread would be a far more efficient method for running game logic and painting? How many frames per second are you getting/need? What type of canvas are you extending? Have you tried implementing your game using a thread architecture combined with a GameCanvas (MIDP2.0)?
Also, do you have any screen shots of the current implementation?
Regards,
Ribot
when i converted the timer into a thread, the thread stops/hangs when i press a key. i tried to put the keyPress method inside the run() but it doesn’t work still. i have at at most 5 frames for player’s walking, attacking…monster’s walking, attacking etc. I extended the GameCanvas and tried to implement Runnable. :-/
I’ve seen most of the mobile games are implemented and have their game loop using thread. i wonder if it’s ok to use timers instead. ???
Timers just use Threads anyway, so there shouldn’t be much difference in performance except for maybe a very slight overhead cost for the code in the Timer methods.
Have you checked out Forum Nokia’s MIDP 2.0 Game API example, the Sheepdog game? It uses GameCanvas and Threads, and doesn’t hang. You could download its source code and compare with what you’re doing. If you have any questions about its design, you could ask here: I’m the author of that example (and the voice of the sheep!).
David
I get very smooth animation when using a GameCanvas plus thread. The run method of my thread looks something like this:
while(isRunning) {
processKeyPresses();
tick(); // logic code - tick the controller.
if(isShown) {
if(!isPainting) {
paintWorld();
flushGraphics();
}
}
try {
Thread.sleep(ANIMATION_DELAY);
} catch (Exception ex) {}
Were you getting the pauses when testing on the device as well as the emulator. Also, what are your methods that keyPressed() calls doing? Sounds like your doing something intensive, which shouldn’t be in a keypress.
Regards,
ribot