I'm making a game where I need events to happen in a precise amount of time. Explanation : I want that event A happens at 12000ms, and event B happens every 10000ms.

So "if"s should looks like this.

//event A
if(Ticks == 12000) //do things
//even B
if(Ticks % 10000 == 0) //do stuff

But now how can I have this "Ticks" value ? I tried to declare an int and then increasing it in the update method, I tried 2 ways of increasing it :


It doesn't works because the update method is not always called every microseconds.

Ticks += delta;

It's kinda good but the delta is not always equals to 1, so I can miss the precise values I need in the if statements

So if you know how can I do events in a precise amount of time please tell me how can I do this

  • \$\begingroup\$ stackoverflow.com/questions/1770010/… maybe this can help \$\endgroup\$
    – Heckel
    May 26, 2014 at 19:52
  • \$\begingroup\$ I saw that I should use something like ticks = System.nanoTime() but when I start the game and I look at the value its already a very big value \$\endgroup\$ May 26, 2014 at 20:19

1 Answer 1


It's a simple change to logic really, simply use a >= test instead. If Ticks is greater than or equal to your trigger amount, you should fire the trigger. Then you use the Ticks += delta; to increment it. Additionally, you can create a more generic object to hold information for a single event.

class EventObject {
    float TimeSinceTrigger = 0;
    float TriggerInterval;
    boolean FireOnce;
    boolean Fired;

    public EventObject(float triggerInterval, boolean fireOnce) {
       TriggerInterval = triggerInterval;
       FireOnce = fireOnce;

    public void update(float delta) {
        TimeSinceTrigger += delta;

    public bool isReady() {
        if(FireOnce && Fired)
            return false;

        if(TimeSinceTrigger >= TriggerInterval) {
            //We're ready, if we fire only once, set our flag for that
            // otherwise, reset our time since trigger to fire again later.

                Fired = true;
                TimeSinceTrigger = 0;

            return true;
        return false;


Then you can use that object like:

//Instantiate the objects
EventObject repeatingEvent = new EventObject(1000, false);
EventObject singleFireEvent = new EventObject(1000, true);

//At the beginning of your update loop:

//Wherever else in your update loop

if(repeatingEvent.isReady()) {

if(singleFireEvent.isReady()) {

With that, you're on your way to a robust event system. It could certainly use a lot of improvement. For example, keeping the event objects in a list, so you can stop updating them if they're done.

As for getting accurate delta time, you're on the right path. You System.nanoTime() would be used like:

long lastUpdate = System.nanoTime();    

//In the update method

long currentTime = System.nanoTime();
long delta = currentTime - lastUpdate;
lastUpdate = currentTime;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .