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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

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

Post 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

Last edited by eqnox on Wed Aug 19, 2015 9:52 pm, edited 1 time in total.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post by Jasoco »

You should tell us what line it happens on. And you should check that line yourself.
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

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

Post by louie999 »

Maybe you set "xvel" to nil ? or that value is really nil.

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

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

Post 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"
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

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

Post by Sosolol261 »

OP is quiet...
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests