So I have an infinite runner where the player moves along to right. The Player is a Physics based object. So it is possible for the player to move backwards. I need a way to keep track of how far the player has moved along the X axis. How would I track that?
You can have a variable to store the time of moving.
Then when the player starts moving, you add dt to it.
When the game over, stop adding and you have the distance = player speed * time of moving.
Okay I understand in concept, but how would I construct that syntax wise. Snice it's a physics object, I'm not sure if the variable for current speed would have to be tried to the object physics velocity.
You don't have to worry about time at all. You can literally just count x pixels moved.
Have a variable that stores the last x position, and total distance travelled.
Every tick
Set distanceTravelled to currentX-lastX
Set lastX to currentX position
Develop games in your browser. Powerful, performant & highly capable.
Okay so I have the variable recorded like this
CurPosition - Every tick: Set Value player.X
Then how do I record it's last X position so I can match your post?
set distance to player.x + player.oldx
set oldx to player.x
if you write them in that order it will calculate the distance travelled
Okay so I make two variables, Distance_Traveled and oldx
Then every tick set oldx to player.x? Cause player.oldx gives me a syntax error.