Page 1 of 1

[solved] can anyone find why I am getting an error in this.

Posted: Tue Aug 18, 2015 10:55 pm
by eqnox
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.

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


Re: can anyone find why I am getting an error in this.

Posted: Wed Aug 19, 2015 7:26 am
by Jasoco
You should tell us what line it happens on. And you should check that line yourself.

Re: can anyone find why I am getting an error in this.

Posted: Wed Aug 19, 2015 9:53 am
by louie999
Maybe you set "xvel" to nil ? or that value is really nil.

Re: can anyone find why I am getting an error in this.

Posted: Wed Aug 19, 2015 3:55 pm
by NickRock
In main.lua did you call the function player.LOAD() or player.load() ?

Edit:

And are you sure you've included the player object in main.lua like this?:

Code: Select all

require "player"

Re: can anyone find why I am getting an error in this.

Posted: Wed Aug 19, 2015 4:42 pm
by Sosolol261
OP is quiet...

Re: can anyone find why I am getting an error in this.

Posted: Wed Aug 19, 2015 9:51 pm
by eqnox
sorry I got busy with school stuff. Thanks everyone for responding, but just like the error came (without reason) it is now gone. I have no idea why I got an error, but now it is fine. Thanks everyone.