Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  stopwatch not working  (Read 852 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Duke





« Posted 2011-04-03 22:40:44 »

I know there's good alternatives such as the GAGE timer, but I've just taught myself Java(ish) and wrote this program to get my head around some of the basics, can someone tell me why this program goes so catastrophically wrong when I run it?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
 public static void main(String[] args) {
        Runnable ticker = new Timer();
        Thread t = new Thread(ticker);
        t.start();
    }

}

class Timer implements Runnable {
    long oneClock;
    long twoClock;
    int gameLength = 25;
    long diff;
    long beforeTime;
    long afterTime;
    long realTime;
    boolean loop = false;

   public void run () {
        beforeTime = System.nanoTime();
        while (gameLength > 0){
            oneClock = System.nanoTime();
            loop = true;
           
            while (loop = true) {

                try {
                Thread.sleep(10); } catch (InterruptedException ex) {ex.printStackTrace(); }
                twoClock = System.nanoTime();

                diff = twoClock - oneClock;
                if (diff >= 1) {
                gameLength -= diff;
                System.out.println (this.gameLength) ;
                loop = false;
                } //second while close
            }
       } // 1st while close

        afterTime = System.nanoTime();
        realTime = afterTime - beforeTime;
        System.out.println("The end of the clock");
        System.out.println("The clock took" + realTime + "seconds to finish");
    }
}


That's the code, this is the output, which can tell is wrong:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
-9577724
-29163946
-58738110
-98324597
-147897743
-207477652
-277060474
-356637654
-446223307
-545811873
-655405063
-774993039
-904580078
-1044161048
-1193738515
-1353326594
-1522912453


I admit before I get flamed, I am a noob, but I'd appreciate some direction. Cheers guys.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-03 22:49:00 »

This code assigns 'true' to 'loop'...
1  
while (loop = true) {


You should replace it by:
1  
while (loop == true) {


Or even better:
1  
while (loop) {

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline zlandorf
« Reply #2 - Posted 2011-04-03 22:56:19 »

1  
 System.out.println("The clock took" + realTime + "seconds to finish");


Watch out, you're working with nanoseconds there, not seconds.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Duke





« Reply #3 - Posted 2011-04-03 23:28:37 »

ha lol, can't believe I missed that thing with loop!!

I know I was working with nanoseconds, and in a previous version I multiplied the results by 1000000000 but I thought that might have been a root with the problem for some reason and edited that out.

ok so I changed the loop and multiplied each nanoTime by 1000000000 and now it gives me this result:
1  
2  
3  
4  
5  
6  
run:
767364121
-977696743
The end of the clock
The clock took19850560000000000seconds to finish
BUILD SUCCESSFUL (total time: 0 seconds)

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-04-03 23:31:46 »

ok so I changed the loop and multiplied each nanoTime by 1000000000 and now it gives me this result:

So now you're using atto-seconds.

What about dividing by 1000000000?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline westloar

Junior Duke





« Reply #5 - Posted 2011-04-04 12:27:42 »

I discovered whythe stop watch was going crazy, I should have been dividing the nanoTime no multilying it

When I changed this, it worked, apart from it takes 24secs to finish not 25 :/ but that's a minor setback!

Cheers for the help Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!