Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  An easy way to count up at a constant (frame-independant) rate.  (Read 6113 times)
0 Members and 1 Guest are viewing this topic.
Offline Computerology

Senior Devvie


Medals: 5
Projects: 2
Exp: 4 years


"Gray areas" don't exist. Find your truth.


« Posted 2015-01-15 04:23:09 »

Hi all,

I wanted a more convenient way to do cooldown timers in my game (and just add numbers together at a certain rate without manually using delta time and without the hassle of using really small constants with it), so I made this little snippet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static double getConstant(double amount_to_add, double per_x_seconds) {
    if (per_x_seconds <= 0) {
        System.out.println("Time cannot be less than or equal to 0!");
        return 0;
    }
    double time_in_mills = per_x_seconds * 1000;
    double amount_to_add_per_frame = amount_to_add / time_in_mills;
    amount_to_add_per_frame=amount_to_add_per_frame * deltaTime;
    return amount_to_add_per_frame;
}


It will return a constant that increases/decreases a variable to the number you specify in the time you specify.

e.g.

1  
2  
3  
4  
double x = 0; 
while (x < 5) {
    x+=getConstant(5, 10); //in 10 seconds, x will be 5
}


It includes delta time as well, and you can easily change it to make delta a parametre if you want. I know this is simple but it is very useful to me and I wanted to share. Have a good day Smiley

edit: Added an error if the specified time span is <=0 (thanks, Riven!)

Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2015-01-16 08:25:25 »

Make your code stricter. If per_x_seconds <= 0.0, you should throw an exception, as the callsite is clearly in an invalid state. Never have silent errors, this is neither PHP nor C. Pointing

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

Senior Devvie


Medals: 5
Projects: 2
Exp: 4 years


"Gray areas" don't exist. Find your truth.


« Reply #2 - Posted 2015-01-16 13:26:32 »

Noted, thanks!  Cool

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

JGO Knight


Medals: 52


From rags to riches...to rags.


« Reply #3 - Posted 2015-01-16 14:27:40 »

I think Riven meant a little stricter than a System.out.println(...) statement. Something along the lines of throwing an actual exception so the code can gracefully fail instead of continuing to run in an erred state. Wink

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline Computerology

Senior Devvie


Medals: 5
Projects: 2
Exp: 4 years


"Gray areas" don't exist. Find your truth.


« Reply #4 - Posted 2015-01-16 15:17:33 »

I think Riven meant a little stricter than a System.out.println(...) statement. Something along the lines of throwing an actual exception so the code can gracefully fail instead of continuing to run in an erred state. Wink

Ah, I see. Although in my case, returning 0 isn't an error. In my code I have a statistic called Obnoxiousness, which is a scale from 0-100. Sheep will bleat more if their obnoxiousness value is higher. If their obnoxious value is 0, they will not make any noise.

Offline CodeHead

JGO Knight


Medals: 52


From rags to riches...to rags.


« Reply #5 - Posted 2015-01-16 15:34:24 »

Fair enough; however, I think the initial point was to make it a tad more robust for others who may wish to utilize your code. Their use cases may not be the same. It's just a suggestion though. Sharing with the community is always an appreciated gesture and people can modify as their needs dictate. Smiley

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline Archive
« Reply #6 - Posted 2015-01-17 17:11:09 »

Ah, I see. Although in my case, returning 0 isn't an error. In my code I have a statistic called Obnoxiousness, which is a scale from 0-100. Sheep will bleat more if their obnoxiousness value is higher. If their obnoxious value is 0, they will not make any noise.
Check if it's <= 0 not just ==0.

Offline Computerology

Senior Devvie


Medals: 5
Projects: 2
Exp: 4 years


"Gray areas" don't exist. Find your truth.


« Reply #7 - Posted 2015-01-19 03:00:36 »

Tsk. How sloppy of me. I'll change it. Grin

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04: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!