I'm trying to make a player move around, I'm doing it the same way i always do, yet for some reason now, xVel is nil :/ can someone find an issue im missing?
player = {}
function player:load(x,y)
self.width = 128
self.height = 128
self.speed = 500
self.xVel = 0
self.yVel = 0
self.acceleration = 1500
self.friction = 750
self.x = x
self.y = y
self.grounded = false
end
function player:update(dt)
self:move(dt)
self:control(dt)
end
function player:move(dt)
self.x = self.x + self.xVel * dt
self.y = self.y + self.yVel * dt
end
function player:control(dt)
if love.keyboard.isDown("left" or "a") then
if self.xVel < -self.speed and self.xVel - self.acceleration * dt < -self.speed then
self.xVel = self.xVel - self.acceleration
elseif self.xVel < -self.speed and self.xVel - self.acceleration * dt > -self.speed then
self.xVel = -self.speed
end
end
end
function player:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
function LOAD_PLAYER(x,y)
player:load(x,y)
end
function UPDATE_PLAYER(dt)
player:update(dt)
end
function DRAW_PLAYER()
player:draw()
end
The problem isn't in that code. It's somewhere else in the project. Most likely you're trying to update the player before it's been loaded. But we can't tell without more information.
Jasoco wrote:The problem isn't in that code. It's somewhere else in the project. Most likely you're trying to update the player before it's been loaded. But we can't tell without more information.
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself
This is the best way of learning! Now you'll always check it twice.
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself
This is the best way of learning! Now you'll always check it twice.
XD yeah i will, ill like stare it down for 5 minutes everytime before advancing from now on Xd
The way you could've/should've found that yourself is by working backwards from the problem: you see the error mentioning xVel, so you try to print it in that function for debugging. It's nil, so you put a print where you thought you set it (player:load()) and are surprised to see it never gets executed. Working back from that you would put a print in LOAD_PLAYER, then love.main, at which point you would've probably realized your mistake.
s-ol wrote:The way you could've/should've found that yourself is by working backwards from the problem: you see the error mentioning xVel, so you try to print it in that function for debugging. It's nil, so you put a print where you thought you set it (player:load()) and are surprised to see it never gets executed. Working back from that you would put a print in LOAD_PLAYER, then love.main, at which point you would've probably realized your mistake.
Just want to say that this is the important thing to take away from it all. Everyone makes these kinds of misstakes, but if you know proper debugging you will find it quickly. Print a lot and explain your code to yourself (or do what I do and use a duck: https://en.m.wikipedia.org/wiki/Rubber_duck_debugging)
I personally vouch for the rubber duck method. Whether with a duck, a person, or just to an imaginary friend. Use the ELIF method. (Explain like I'm five)