-1

I have a timer based on System.nanoTime() and I want to slow it down.

I have this:

elapsed = System.nanoTime();
seconds = ((elapsed -  now) / 1.0E9F);

where now is System.nanoTime(); called earlier in the class.

In order to slow it down I tried:

if(slow){
    elapsed = System.nanoTime();
    seconds = ((elapsed -  now) / 1.0E9F) * 0.5F;
}else{//The first code block

But then I run into the problem of seconds being cut in half, which is not desirable. Before slow is true, it's displaying 10 seconds to the screen, and when slow is true, it displays 5 seconds, which is not desirable.

I want to slow the timer, not cut it in half as well as slow it down

On the other hand, it slows down the time, which is what I want.

How would I slow down the time, without cutting my existing time in half?

  • 1
    Your question doesn't make sense. If you multiply seconds by 0.5 then of course it halves the number of seconds. Perhaps you want two counters, one measuring "real" time and the other one which may be slowed? – davmac Jul 27 '11 at 05:59
  • What don't you understand about my question? I want to slow down the timer that runs off of `seconds`, but not halve the timer in the process –  Jul 27 '11 at 06:03
  • If the problem is that you are slowing it down too much, just change the 0.5F to something else (say 0.9F). If the problem is that you want "seconds" to be accurate, but you have some event which should occur after a particular time and you want that to be adjustable according to the value of "slow", then you should just reduce "seconds" by some factor (if slow is true) when you're checking whether the time has elapsed. If it's neither of these then I don't understand the problem; you need to explain it better. – davmac Jul 27 '11 at 06:14
  • I edited the question in an attempt to explain my problem better. I am keeping track of `seconds` and displaying it to the screen. When slow is activated, I do not want `seconds` to be halved. I only want it to halve the time in between each elapsed second. –  Jul 27 '11 at 06:16
  • @Whoever voted down my question, could you explain why? I'm sure it would help me to ask better questions in the process. I only want to slow down my timer, not halve it. Is this really such a bad question? –  Jul 27 '11 at 06:25
  • it wasn't me who voted your question down; but really, it is not well worded. You don't want to "slow down the time", what you want to do is *sometimes* slow the rate of increase of a counter, which should normally increase at a rate of 1 per second, but which sometimes should increase at a slower rate. Unless I still misunderstand you. – davmac Jul 27 '11 at 06:33
  • @OWiz The problem is we don't have context. What does your timer do? Is it a `java.util.Timer`? The definition of a "slow timer" hinges on what the timer does! –  Jul 27 '11 at 06:44
  • You should clarify your question. I think I know what your mean. You say you don't want the total time in calculated by the timer got multipied by half. My understanding is that you want the timer to calculate in different speed, and not messing with the existing elapsed time. Right? – lamwaiman1988 Jul 27 '11 at 06:44
  • And can you post your complete code here? – lamwaiman1988 Jul 27 '11 at 06:44

2 Answers2

2

Add to seconds rather than recalculating it from scratch each time through the loop:

elapsed = System.nanoTime();
if (slow) {
    seconds += ((elapsed -  now) / 1.0E9F) * .5F;
}
else {
    seconds += ((elapsed -  now) / 1.0E9F);
}
now = elapsed;  // so next time you just get the incremental difference
davmac
  • 20,150
  • 1
  • 40
  • 68
  • Thank you, this was just what I needed. I'm sorry for any confusion my question caused –  Jul 27 '11 at 07:18
0

You can have a member variable that contains the multiply needed to be done. Always calculate the correct number of seconds, and when you use the timer, multiply it:

float multiple = 1.f;
//...
seconds = (elapsed -  now) / 1.0E9F;
//...
// setting timer:
somefunc(seconds * multiple);

public void setSlow(boolean slow) {
    if(slow)
        multiple = 1.0f;
    else
        multiple = 0.5f
}
MByD
  • 135,866
  • 28
  • 264
  • 277