Hello! I'm a relatively new programmer. I learned how to use lua A LONG time ago with a little game called Roblox . Having moved to making my own 2d games with Love2d, I have encountered my first major problem. I can't seem to get self.(field) to be recognized as a value of the class. Here is my code for the class.
require("lib/SECS")
Circle = class:new()
function Circle:init(dt)
self.xspeed = 0
self.yspeed = 0
self.x = love.graphics.getWidth()/2 - 25
self.y = love.graphics.getHeight()/2
self.radius = 25
end
function Circle:draw()
love.graphics.setColor(255,0,0)
love.graphics.circle("fill",self.xpos,self.ypos,self.radius,50)
end
function Circle:update(dt)
if self.y >= (love.graphics.getHeight() - self.radius) then
self.y = love.graphics.getHeight() - self.radius + 1
end
Circle:processKeys()
Circle:processSpeed(dt)
end
function Circle:processKeys()
self.yspeed = 0
self.xspeed = 0
if love.keyboard.isDown( "w" ) == true then
self.yspeed = -100
end
if love.keyboard.isDown( "s" ) == true then
self.yspeed = 100
end
if love.keyboard.isDown( "a" ) == true then
self.xspeed = -100
end
if love.keyboard.isDown( "d" ) == true then
self.xspeed = 100
end
end
function Circle:processSpeed(dt)
self.y = self.y + self.yspeed * dt
self.x = self.x + self.xspeed * dt
end
My error will usually be something like "attempt to perform arithmatic on field 'blah' (a nil value). Anybody know what's wrong? If you need to see the full code please request.
Last edited by xXOdysseusXx on Sun Nov 13, 2011 7:51 pm, edited 1 time in total.
function Circle:init(dt)
self.x = love.graphics.getWidth()/2 - 25
self.y = love.graphics.getHeight()/2
self.radius = 25
end
function Circle:draw()
love.graphics.circle("fill", self.xpos, self.ypos, self.radius, 50)
end
Do you see it now?
Generally speaking, the error screens in Love are actually really useful. If they say they have an error with a variable name, tracking down that variable will often reveal your bug. This is why consistent, descriptive variable names are so important.