Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  Trouble Understanding Timers  (Read 1393 times)
0 Members and 1 Guest are viewing this topic.
Offline crothersb

Junior Newbie





« Posted 2009-07-23 20:58:02 »

I have only been programming in java for about three months so this is going to be a fairly easy question to answer!

I am trying to create a section of code that runs every 10 seconds. For example a simple animation that accurs while everything else is going on. I thought it would be as simple as importing the Timer class and then creating a object of that timer class, setting the interval and being done with it. However I can't seem to get it to work right. Any examples of reponses will be welcome.
Offline markmistry

Junior Member





« Reply #1 - Posted 2009-07-23 21:19:28 »

 this is how i have it in my game...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
 

   public Timer timer;
   
   public static int newtimer =10000; // 10000 milliseconds
 
   
   timer = new Timer(newtimer);
   
   if (timer.action(elapsedTime)) {
   
         //do something
  )


 



or you could try a thread i use that too


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  
46  
47  
48  
49  
50  
51  
import java.io.*;
import java.util.Date;

public class hThread extends Thread{

      public static int hmins =2;
      public static int hsecs =59;

       public void run(){

   Date d1 = new Date();
         
   for(;hmins >0;){
           
      for (;hsecs>0;){
         
            try{  
                   
         Thread.sleep(1000);
      }
           
      catch (InterruptedException e) {

         e.printStackTrace();
      }            
      System.out.println(hmins+":"+hsecs);
      hsecs--;

      if(hsecs ==0){
                     
      hsecs =59;
      hmins --;  
      }
               
      if(hmins ==-1){
                       
         Date d2 = new Date();
         long diff = (long)d2.getTime() - d1.getTime();
         DateDiff.secs = (int)diff/1000;
         DateDiff.mins = DateDiff.secs/60;  
         System.out.println("difference in seconds : " + DateDiff.secs);  
         System.out.println("difference in minutes : " + DateDiff.mins);
         return;
      }
   }
               
}

     
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;

public class DateDiff {

public static  int secs;
public static  int mins;

  public static void main(String[] args) {
   
       try{
     
         new hThread().start();
         
      }

      catch (Exception e) {
         
         System.out.println("error starting hthread");
      }
   
  }
}
Offline crothersb

Junior Newbie





« Reply #2 - Posted 2009-07-23 21:37:17 »

import javax.swing.Timer;

public class newTimer
     public Timer timer;
     public static int newTimer=1000;

     public void testingmethod()
     {
            timer=new Timer(newTimer);

            if (timer.action(elapsedTime))
            {
                        //do something
             }

      }//end testingmethod
}//end class


What is wrong with the above code?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline markmistry

Junior Member





« Reply #3 - Posted 2009-07-23 23:47:23 »

your missing a bracket from your class for a start and i should have mentioned i use gtge so the timer(elapsedtime) wont work i dont think.
Its easier using a thread..some brain box will show you an easier way im sure.
Ive just gone and look at the api for timer and now my brain hurts ..its too complicated
Offline HELL

Junior Newbie





« Reply #4 - Posted 2009-07-26 18:11:05 »

I don't kno whats wrong with your code but I always use the swing timer like this (see below) and it works a lot for me.

Timer we;//your timer
int interval = 10000;//10 seconds
we = new Timer(interval, new ActionListener(){
public void actionPerformed(ActionEvent e){
//do something here
}
});

//here we tell it to start
we.start();
//this optional, if x is true, then the timer will repeat infinitively
//every y seconds, where y is the delay (or interval)
//otherwise it'll wait y seconds and do its job once and stop
we.setRepeats(boolean x);

Maybe you'll have some problems accesing variables inside the inner actionlistener, the compiller will say that you need to declare the variable final) But you can make your own class then add any variable you may need as global and make them point to anyother variable that you may need to use inside the actionlistener

Something else that you should consider is that  if your going to put some animation lets say.. inside your game loop then you have to tell it that when your timer starts running don't start it again anymore otherwise it'll make it start over and over again causing undesired behavor, you can use a boolean for that purpose or access the timer method isRunning().


Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2009-07-26 20:36:04 »

1  
2  
3  
4  
5  
6  
7  
8  
double lastWhatever;
//blablabla

if(System.currentTimeMillis()-lastWhatever = 1000*(however long you want(seconds)))
{
//whatever you want to do
lastWhatever = System.currentTimeMillis();
}

I know this is crude but it has worked for me so far. Of Course I am counting on a main loop so this gets called every once and a while.
Offline fermixx

Senior Newbie





« Reply #6 - Posted 2009-08-15 04:01:30 »

I know this is crude but it has worked for me so far. Of Course I am counting on a main loop so this gets called every once and a while.


same here.

anyways its a pitty that it has to be checked like 100 times per second. if i want to make things to happen every 10 minutes (600 seconds), it is checked 60000 times wasting time for nothing...

Sad

RTS game or a big rock-paper-sissors ?
Offline markmistry

Junior Member





« Reply #7 - Posted 2009-08-15 11:00:05 »

thats why i like the thread approach totally independant of game loop

1  
2  
3  
4  
5  
6  
7  
8  
9  
 
try{  
     Thread.sleep(1000);
}
           
catch (InterruptedException e) {

      e.printStackTrace();
}


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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!