[solved] can anyone find why I am getting an error in this.
Posted: Tue Aug 18, 2015 10:55 pm
I keep getting an error in this code the error is. attempted to preform arithmetic on "xvel" (a nil value).
This confuses me because this same code was working just a few minutes ago.
This confuses me because this same code was working just a few minutes ago.
Code: Select all
player = {}
--this is where we set atributes of the player
function player.load()
player.x = love.graphics.getWidth()/2
player.y = love.graphics.getHeight()/2
player.width = 10
player.height = 10
player.xvel = 0
player.yvel = 0
player.friction = 1
player.speed = 500
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
--this is where the player is drawn from
function player.draw()
love.graphics.setColor(0,255,0)
love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)
end
--this is where the phusics are handled
function player.physics(dt)
player.x = player.x + player.xvel * dt
player.y = player.y + player.yvel * dt
player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))
end
--this is where the movement is handled
function player.move(dt)
if love.keyboard.isDown("d") and
player.xvel < player.speed then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown("a") and
player.xvel > -player.speed then
player.xvel = player.xvel - player.speed * dt
end
if love.keyboard.isDown("s") and
player.yvel < player.speed then
player.yvel = player.yvel + player.speed * dt
end
if love.keyboard.isDown("w") and
player.yvel > -player.speed then
player.yvel = player.yvel - player.speed * dt
end
end
function player.collision(dt)
if player.x < 0 then
player.x = 0
player.xvel = 0
end
if player.y < 0 then
player.y = 0
player.yvel = 0
end
if player.x + player.width > love.graphics.getWidth() then
player.x = love.graphics.getWidth() - player.width
player.xvel = 0
end
if player.y + player.height > love.graphics.getHeight() then
player.y = love.graphics.getHeight() - player.height
player.yvel = 0
end
end
--functions are put here to be easily managaed in the main file
function player.LOAD()
player.load()
end
function player.UPDATE(dt)
player.physics(dt)
player.move(dt)
player.collision(dt)
end
function player.DRAW()
player.draw()
end