Question

I looked up many threads on how to use delta time and I can't figure it out.

I want my guy to move at a constant speed every time I run the game.

So I have:

timer = new Timer(9, this);
timer.start();
then the paint method.

I tried doing start time at the beginning of the paint method and then a current time at the end of it. How would I make it affect the timer's speed or how far the player moves. I've tried multiplying the time delta by the increment to the x. nHeroX = dx*deltaTIme;

I feel like it's very simple I just need someone to tell me straight what to do please.

Was it helpful?

Solution

Traditionally, game programming relies on a continuous loop. Anyway, all I have realized.

In this loop, you should separate model update and graphic update. In your snipset, you seem to try mixing the two things.

In this loop :
- In a first time, you let the model to update.For example : guy's move from x to x1.
- In a second time, you must refresh the graphics to allow the guy to have a new position in the screen.

The speed of your guy depends on the guy model (ex: constant in your guy class). But the speed of another type of guy could be different.

The general speed of your game relies on the FPS set in your main loop.

You can see an example on this post : Java Main Game Loop

Good luck

OTHER TIPS

  1. Get the system time from the System object.
  2. Get it again during your animation. Delta time is nothing more than subtraction.
  3. Get rid of the JavaScript tag. The languages are completely different.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top