Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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 1252 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-05-18 00: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 00: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
« Reply #2 - Posted 2012-05-18 01:19:55 »

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

My code never has bugs... it just develops unexpected features!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #3 - Posted 2012-05-18 12: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 13: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2012-05-18 17: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.

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!