Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  My timer class won't work.  (Read 899 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2013-06-29 22:36:50 »

Hi!
So, I created a timer class. Here it is:
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  
public class Timer implements Runnable {
    public long timemillis;
    public Thread runningthread;
    public boolean done;

    public Timer(float timemillis) {
   this.timemillis = (long) timemillis;

   done = false;
    }

    public void start() {
   if (runningthread == null || !runningthread.isAlive()) {
       runningthread = new Thread(this);
       runningthread.start();
   }
    }

    @Override
    public void run() {
   try {
       Thread.sleep(timemillis);
   } catch (InterruptedException e) {
       e.printStackTrace();
   }
   done = true;
    }

    public void setTimer(long timemillis) {
   this.timemillis = timemillis;
    }

    public boolean get() {
   boolean tempdone = done;
   done = false;
   return tempdone;
    }
}


I have an issue. When I use it, it works for a while. It runs, and I do get() to check if it is done, if so: start(). I do that a few times in the code, but all of the sudden, it stops working. It does not ever get done when I check through get(). What is happening?

Also, if you see a flaw in my code, do tell. I do not have any performance issues though (while running a lot of these never drops below 57fps!).

Offline HeroesGraveDev

JGO Kernel


Medals: 256
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-06-29 22:41:39 »

I don't see any loop in there...

EDIT: I believe you are starting a new thread every time you call start().

Bad idea.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #2 - Posted 2013-06-29 22:45:51 »

This is just the class. I have this code inside every parent's update
1  
2  
3  
4  
if (timerexample.get()) {
timerexample.start();
Action()
}


Also, like I said: no issues w/ performance while there are tons of these running. Every start() makes a new thread, replacing the old one. That way I dont get 100 different threads...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 256
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-06-29 22:46:33 »

Why not just use one thread?

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #4 - Posted 2013-06-29 22:47:10 »

gotta time a lot of things. Can I do that w/ one thread?

Offline HeroesGraveDev

JGO Kernel


Medals: 256
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-06-29 22:49:30 »

Yes (well, sort of), but what are you actually using this for?

Before I write up some code for you it might be a good idea to make sure you're doing this the right way.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #6 - Posted 2013-06-29 22:55:09 »

I use this for 2 things so far.
#1:
I have an enemy. He will wander if a player is not present. When he wanders, he chooses a random direction, and starts a timer. When said timer goes off, he will choose another direction, and restarts the timer!
#2
I have a player. Said player will press a button to shoot. I do not want player to hold down the button to shoot rapidly. I want it to shoot! 1, 2, 3, shoot! 1, 2... So I use the timer. I set the timer, and if the timer is done, then the player will be able to shoot, and the timer will restart.

Offline HeroesGraveDev

JGO Kernel


Medals: 256
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2013-06-29 22:59:28 »

Don't use Threads for that.
I fact, never use Threads unless you are absolutely sure you need to.

Use "tick counters" like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
int tick_count;

// update() is called each frame from the main loop
public void update()
{
   tick_count++;
   
   if(tick_count == target_tick)
   {
      doAction();
      tick_count = 0;
   }
}


target_tick
is the amount of frames that pass between the actions.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #8 - Posted 2013-06-29 23:01:43 »

Ok!

Offline xmakina

Senior Newbie





« Reply #9 - Posted 2013-06-30 16:49:50 »

Whenever someone talks about using threads, I always like to recall this joke:

"A programmer had a problem. He thought to himself, 'I know, I'll solve it with threads!'. has Now problems. two he" - Chris Geiersbach
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.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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!