Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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  
  Timers  (Read 825 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21

Senior Member


Medals: 4
Projects: 1



« Posted 2012-05-18 02:31:33 »

I'm looking for the best way to make a reload system a gun in my game, however, i'm not sure about the best way to go about this. I know there is a timer variable, but i'm unsure of how i should use it. Or possibly I should use nanotime(), I don't know. Any input on this conundrum?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ReBirth
« Reply #1 - Posted 2012-05-18 02:41:57 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private long reload = 2000; //2 sec

public void update(long delta){
//update logic
reload -= delta;
if (reload <= 0){
reload = 2000;
player.reload();
}
}

Offline zngga

Junior Member


Medals: 3


Current Project: Ascension (infinite RPG)


« Reply #2 - Posted 2012-05-18 03:19:55 »

ReBirth has the right idea here. I always use delta timing.

My code never has bugs... it just develops random features!
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #3 - Posted 2012-05-18 14:30:06 »

Hi

I'm looking for the best way to make a reload system a gun in my game, however, i'm not sure about the best way to go about this. I know there is a timer variable, but i'm unsure of how i should use it. Or possibly I should use nanotime(), I don't know. Any input on this conundrum?
I hope I really understand what you mean. Personally, I recently succeeded in implementing the selection of a weapon (the "put back" and the "pull out") and the reload in my first person shooter. As you talk about a gun, I assume we speak about the same thing.

At first, if you plan to use nanotime or currentTimeMillis, you will need a "hack" to force the use of an high precision timer under Windows:
http://bugs.sun.com/view_bug.do?bug_id=6435126

Secondly, princec already explained how to use a timer in general and it is used in that way in Ardor3D as far as I know. At each iteration:
- update the timer itself
- update the whole logic
- render

Thirdly, I assume Rebirth's suggestion uses as a parameter the period between 2 frames in seconds (or time per frame). It should be enough.

Finally, I use a more complicated system in order to prevent the player from doing absurd things. For example, you don't want to allow your player to reload and select another weapon at the same time or to reload and attack at the same time. That's why I use a state machine with a kind of "scheduler". I will probably publish an article about that in my blog soon, it is a bit difficult to explain it without a schema.

Offline ReBirth
« Reply #4 - Posted 2012-05-18 15:10:35 »

If you want to follow mine, maybe you can make a separate class to handle timing. But my pseudo one is enough for simple case.

Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #5 - Posted 2012-05-18 19:51:40 »

Look, a tutorial! Smiley

http://www.java-gaming.org/topics/game-loops/24220/view.html

Get yourself a good game timer. From the main timer, call each of your entities or whatever with some update function (with a variable timestep, pass in the delta, with a fixed timestep, pass in nothing). Then have a local value (a float or long or whatever) and in a variable timestep add the delta every frame. In a fixed timestep, add the global delta. When this local value is >= the amount of time you want to wait for, do something.

All done.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (130 views)
2013-05-17 21:29:12

alaslipknot (139 views)
2013-05-16 21:24:48

gouessej (169 views)
2013-05-16 00:53:38

gouessej (162 views)
2013-05-16 00:17:58

theagentd (173 views)
2013-05-15 15:01:13

theagentd (158 views)
2013-05-15 15:00:54

StreetDoggy (202 views)
2013-05-14 15:56:26

kutucuk (226 views)
2013-05-12 17:10:36

kutucuk (225 views)
2013-05-12 15:36:09

UnluckyDevil (229 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.09 seconds with 21 queries.