Friday, 15 July 2011

timer - Run code every X seconds (Java) -


This is not necessarily important, I'm just curious to see what others think. I know it's useless, it's just for fun.

Now I know how to do this, it is quite easy. I am just trying to find a way to do this so that no new variable is required to crowd my class.

Here's how I will:

  Float Time Passed = 0f; Public Zero update () {timepassed + = deltatime; // DeltaTime is just a variable that represents the time passed from one update in seconds to another time (this is a float) if (timepace> = 5) {// code is every 5 seconds time Passed - = 5f; }}  

What do I want to know, if there is a way to do this, the variables passed without time I have a statetime variable (time since the start of the loop) What I use for other things which can be used for it.

If the goal is to actually run the code in every x seconds, then my first option is to use . Timer Another option is to use the Scheduled Exclaration Service, which adds two enhancements when you use it. Timer (better exception handling, for one).

I try to avoid swing. Timer, because I like to leave the EDT (event dispatch thread) uncluttered

Many people write a "sports loop" that is close to what you started. A search on "Game Rope" might depend on you, depending on whether you want to keep a steady rate or not.

Occasionally, in a situation where one does not want to constantly test and reset, one can add two functions through the use of an "end" operation, for example, if you and one integer For 63, you have to repeat through 0-63. It works well on those categories which have the power of 2.

Depending on the structure of your calling code, you can pass the parameter as "statetime" variable and this is your desired x If you do so, then I believe that the call code One step in "Reset" will reset to zero.

Another idea is "starttime" in the update method, after that, your timer will see the difference between current timemales and start-up to see if X seconds have passed or not. Then, the code that calls you, it should probably set a new "start-up" as part of the process. The good thing about this method is that there is no need to increase in the past.

As long as I churn the ideas: future can also create and test "target time" variables if currentTimeMillis () - target time> 0.

Starttime or target time can be irreversible, which depends on the use often, gives a little bit more.


No comments:

Post a Comment