I'm new to coding, and this is my first project, so forgive me if there's something really simple I'm not understanding yet, but I ran into a problem trying to use dt for my character's movement. I created a new file for the character knight, and tried to include all of his movement in his file so I didn't have to write it in main. Here's knight.lua. The problem is in self.move().
Code: Select all
knight = {}
knight.new = function()
local self = self or {}
self.x = 200
self.y = 350
self.xvel = 0
self.yvel = 0
self.gdecay = 0
self.adecay = 0
self.grav = 80
self.weight = nil
self.onground = true
self.move = function(dt)
self.x = self.x + self.xvel*dt
self.y = self.y + self.yvel*dt
if love.keyboard.isDown("d") then
self.x = self.x + 10
elseif love.keyboard.isDown("a") then
self.x = self.x - 10
end
if self.onground == true then
self.y = 350
self.yvel = 0
if love.keyboard.isDown("l") then
self.yvel = -1600
self.onground = false
end
end
if self.onground == false then
self.yvel = self.yvel + self.grav
end
if self.y > 350 then
self.onground = true
end
end
return self
end
Here's my entire main file.
Code: Select all
require("knight")
function love.load()
knight = knight.new()
end
function love.update(dt)
knight:move(dt)
end
function love.draw()
love.graphics.rectangle("fill", knight.x, knight.y, 40, 50)
love.graphics.rectangle("line", 0, 400, 900, 300)
end
I guess my big questions are as follows:
What exactly is dt? What is its classification? I want to know exactly what I can or cannot do with it/where I can or cannot use it and why.
If I can actually code my character's movement in his file, what did I do wrong? I need to use dt for calculations, so is there another way of doing that?
Sorry for the long post. Thanks for reading if you did.