11634

How do I add a timer in libgdx java?

Question:

I am trying to add a timer for power ups in my game. What I am trying to do is change a parameter (like jump height for example) after the player has collided with the power up box but it should only apply for 4 seconds. I have already written the collision coding and have added a random element to choose which power up is activated. I have also added a power up that will not use the timer (adding 1 to the score).

Searching google I found code using various methods like Gdx.graphics.getDeltaTime() and Timer.schedule but I can't seem to get them to work, maybe I am coding them incorrectly. I've been trying to use code like this but when using System.out.println(timeSinceCollision), I keep getting a constant value every time and it doesn't seem to be increasing.

float timeSinceCollision = 0; timeSinceCollision += delta; if(timeSinceCollision < 4f) { // the action } else { // reset }

Answer1:

As I mentioned in my comment above, you can use the CountDownTimer class.

new CountDownTimer(4000, 4000) { public void onTick(long millisUntilFinished) { } public void onFinish() { isPoweredUp = false; } }.start();

Answer2:

You are creating timeSinceCollision every time with value of 0 thats why it never increase.

Take a look at my function. the If condition will be true every 4 seconds.

final float TIME_SINCE_COLLISION = 4; float timeSinceCollision = 0; public void update(float delta) { timeSinceCollision += delta; if (timeSinceCollision >= TIME_SINCE_COLLISION) { timeSinceCollision -= TIME_SINCE_COLLISION; // Do something after 4 seconds. Gdx.app.log("Do something", ""); } }

Recommend

  • .NET get process ID of my application in C#
  • Activate Monotouch
  • EJB stateless - Private members initialisation
  • Magento invalid cron expression
  • How do i do the following curl command in Java
  • How to auto update a record in database?
  • working with bool value from View in javascript
  • How to make a npm command, that executes two commands in the console (one after another)?
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • Are there any libraries for Python to simulate keyboard action?
  • SIP API media codecs
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • SyntaxError: (irb):26: both block arg and actual block given
  • git add error : “fatal : malloc, out of memory”
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • How do I superscript characters in a UIButton?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Extracting HTML between tags
  • Scrapy recursive link crawler
  • Seeking advice on Jetty HttpClient Hang
  • How to rebase a series of branches?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Problems to linebreak with an int in JLabel
  • javaw.exe and eclipse startup problems
  • How to handle AllServersUnavailable Exception
  • Weird JavaScript statement, what does it mean?
  • ORA-29908: missing primary invocation for ancillary operator
  • Google cloud sdk not working when python points python3
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Android Heatmap on canvas or ImageView
  • java string with new operator and a literal