c++ - QTimer setInterval without resetting remainingTime -
i have app written in qt uses qtimer. it's game , actions controlled timer. game includes ability increase\decrease game speed. code increasing speed is
timervalue -= speedupvalue; if (timervalue < maxspeed) { timervalue = maxspeed; } timer -> setinterval(timervalue);
speedupvalue
and maxspeed
constants. same code used decreasing speed. problem setinterval
resets internal timer , therefore if keep rapidly increasing or decreasing speed game never proceeds because remainingtime
being reset. there way set remainingtime
manually or change interval without resetting it?
you can omit timer object altogether, , instead use static method qtimer::singleshot(...)
, , set new single shot on every timer event timervalue
. no matter how timervalue
modified, each timer cycle completed old value , scheduled current value.
as ra mentioned, can achieve same thing keeping timer object, moving setinterval()
timer event, same thing above, keeping timer object, if need reason.
as craig mentioned in comments, first approach ensure timer events don't accumulated, in case game logic takes longer. also, if go asynchronous design, possible next game cycle begin execution before old 1 completed if go continuously running timer.
it regard, best not use continuously running timer, schedule each next game cycle manually, , if need to. example, may not need schedule if game comes end. also, allow finer control on how accurate game speed goes, after game cycle execution completed, can calculate how time remains until next 1 needed, , schedule value, or if execution took long, can schedule immediately.
Comments
Post a Comment