How to track time in Libgdx(android)
The Screen's render(float delta) function gives you the amount of time that has passed by since the last render(..) call, in miliseconds, as float.
Therefore, a timer would just be the following:
public class GameScreen implements Screen
{
private float bonusTimer;
public GameScreen()
{
bonusTimer = 70f;
}
@Override
public void render(float delta)
{
for(float iter = 0; iter < delta; iter += 0.125f)
{
float iterNext = iter + 0.125f;
float currentDelta;
if(iterNext > delta)
{
currentDelta = delta - iter;
}
else
{
currentDelta = iterNext - iter;
}
bonusTimer -= currentDelta;
if(bonusTimer <= 0)
{
bonus();
bonusTimer += 70f;
}
...
}
}
...
}
What you would want to do is set an instance of this GameScreen to be the active Screen in the descendant of the Game, your core class. Look here for more info: https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game
Adjust the logic as needed (for example, having your own GameModel that resets itself upon the start of the game).
LibGDX Java game: Creating a stopwatch
You can store a float playTime
which starts at 0.0f
.
Every render cycle you increase it by float delta
or Gdx.graphics.getDeltaTime()
(its the same).
The you can use a BitMapFont
, for example, to draw it. You can also use Stage
and use an UI-Widged, for example Label
and set its background to a Texture
of a stopwatch. Set the text of the BitMapFont
to the playTime
. A simple version of this:
Render mehtod:
playTime+=delta;
font.draw(Float.toString(playTime), x, y);
Implement Google Analytics for Android LibGDX Game Screens
You should use the Google Analytics sdk only in the android project, not in the core project. In the core project should only be platform-independent code. Don't import android libs into your core project.
If you want to track a screen view within core project you should can achieve that by using an interface. There is a wiki guide for interfacing libgdx
Your interface for tracking screen views could be something similar to this:
public interface Trackable {
public void trackScreen(String screenName);
}
In your android project this method would then implement the Google Analytics tracking code.
Related Topics
Very Large Soap Response - Android- Out of Memory Error
How to Disable Home Button in Android
Noclassdeffounderror at Google Play Services V2 Library
Android Opengl Es Transparent Background
How to Run Service Continuously
Check If User Is Authenticated for the First Time in Firebase Google Authentication in Android
How to Make My Own Custom Dialer in an Android Phone
Is Proguard.Cfg Needed for Library Projects
Could Not Find Com.Android.Tools.Build:Gradle:4.0.1 ( or Any Version )
How to Add Shadow to the Fab Provided with the Android Support Design Library
How to Add Crosswalk Webview in My Own Android Library Module
How to Shutdown an Android Mobile Programmatically
Android Install_Failed_Uid_Changed
Launch Android Application from a Browser Link
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should