function player_move()
if love.keyboard.isDown("left") then
player.x = player.x - (player.speed * dt)
end
if love.keyboard.isDown("right") then
player.x = player.x + (player.speed * dt)
end
end
Which returns the error, dt has a null value (or similar). How can I apply delta time when using functions outside of main?
Thanks.
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
function player_move(dt) --Notice I put it as a parameter here
if love.keyboard.isDown("left") then
player.x = player.x - (player.speed * dt)
end
if love.keyboard.isDown("right") then
player.x = player.x + (player.speed * dt)
end
end
function love.update(dt)
player_move(dt) --And then passed love.update's dt parameter to player_move's parameter
end
It will work then.
The reason you have to do that is because when a parameter is passed into a function, it creates a local variable that is used inside that function. So to pass that local variable to another function, you give that function the variable as a parameter when calling that function. So that way that function can then use it. If you have any other questions man, just ask.
function player_move()
dt = love.timer.getDelta()
if love.keyboard.isDown("left") then
player.x = player.x - (player.speed * dt)
end
if love.keyboard.isDown("right") then
player.x = player.x + (player.speed * dt)
end
end