Timer delay with a for loop (Jump)

Hi,

I’m very new to Defold and this forum!
I’m creating a project for school so I haven’t had a lot of time to deeply look into everything that Defold does. I’ve been struggling to understand the physics and I don’t want to copy and paste everything so I tried making my character jump using a for loop, and just changing the y-coordinates.
However this makes my character just move very fast as this value is changed very quickly, so I was wondering if there was any way to use the timer.delay() to increase the time taken between the y coordinate being changed? Here is my code

    	if pos.y == 84 then
	if isJump == true then
		y_coordinate = 84 
		repeat
			y_coordinate = y_coordinate + 1
			pos.y = y_coordinate
			go.set_position(pos)
			timer.delay(1, false)
		until y_coordinate ==  200
	end
end

This does make my character “Jump”, but as it is so fast, trying to make the player come down again results in nothing.
I’ve tried reading the documentation but for some reason none of it makes sense to me.
If any help could be given that would be great :slight_smile:

What your code does is to update the player position in a loop during a single frame. And that timer.delay() is not used correctly either. You should apply the code inside your loop in the update() function which is called once per frame, and also get rid of the timer.delay().

When you move the player in the update() function you also need to use the dt (delta-time) that is provided to the update() function and multiply the distance you move the player by the delta-time. This is done to ensure that you move the player in a frame rate independent way.

I realise that what I wrote above is a lot to take in. Please ask questions if there is something you don’t understand!

You can see an example of moving a game object in update() and applying the delta-time here: 8 ways movement

The correct way to use a time can be seen here: Repeating timer example

1 Like