Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 815 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2011-04-04 00: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.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-04 00: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

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2011-04-04 00: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 Member





« Reply #3 - Posted 2011-04-04 01: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)

Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-04-04 01: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 Member





« Reply #5 - Posted 2011-04-04 14: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.

CogWheelz (6 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!