How can I do something every second? [LibGDX]

后端 未结 3 607
再見小時候
再見小時候 2020-12-30 11:53

Lets say I want to make a loop or something that prints out, for example, \"Mario\" every second. How can I do this? Can\'t seem to find any good tutorials that teach this a

3条回答
  •  悲哀的现实
    2020-12-30 12:50

    I used the TimeUtils of libgdx to change my splashscreen after 1.5 seconds. The code was something like this:

    initialize:

    long startTime = 0;
    

    in create method:

    startTime = TimeUtils.nanoTime();
    

    returns the current value of the system timer, in nanoseconds.

    in update, or render method:

    if (TimeUtils.timeSinceNanos(startTime) > 1000000000) { 
    // if time passed since the time you set startTime at is more than 1 second 
    
    //your code here
    
    //also you can set the new startTime
    //so this block will execute every one second
    startTime = TimeUtils.nanoTime();
    }
    
    • check out the libgdx API : TimeUtils

    For some reason my solution seemes more elegant to me that those offered here :)

提交回复
热议问题