1
\$\begingroup\$

I am trying to get something to happen, when a spesific amount of time has passed(in my case it is in secounds). I have tried using this method:

   long elapsed = (System.nanoTime()-startTime)/1000000;
   if(elapsed > 1000)
   {
       score++;
       startTime = System.nanoTime();
   }

with the starttime beiing initiated in the constructor. But when I attack(part of the game) the app either:
chrashes
the things the game renders dissapears, and everything turns black.

I have tried asking google, but it in general gave me the save way. Any new improved ideas?

\$\endgroup\$

closed as off-topic by Josh Nov 30 '15 at 16:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Are you certain that the time related computation is responsible for the crashes? \$\endgroup\$ – Vaillancourt Sep 10 '15 at 18:02
  • \$\begingroup\$ Do you use startTime somewhere else, especially when dividing? \$\endgroup\$ – wondra Sep 10 '15 at 18:06
  • \$\begingroup\$ The startTime is only used for this calculation(otherwise, it would not work). Also, i am 100% positive it is this that caused the chrash(removed the calculation and the app worked) \$\endgroup\$ – user69046 Sep 12 '15 at 10:45
  • \$\begingroup\$ Unrelated to your crash, for regular timers you should use startTime += 1000000000;. When you use startTime = System.nanoTime(); your timer will slowly accumulate error every update because now is always a little bit after your timer expires. \$\endgroup\$ – John K Sep 30 '15 at 19:00
0
\$\begingroup\$

what kind of crash does it give you? have you tried to see if the

    (System.nanoTime() - startTime) 

could be responsible?

try outputing it to the console with a

    System.out.printLn(System.nanoTime() - startTime);

also try outputing your elapsed variables and make sure it's not Null.

having a peak at the part of the code that causes the crash (you talked about attacking) might help. right now your code seems quite fine to me so taking it out of context is hard to debug.

\$\endgroup\$