Scrolling Background Function - Self is a nil Value
Posted: Thu Aug 18, 2016 3:09 am
I cannot figure this out, my local self table seems to be disappearing into thin air for no apparent reason. I think this is a pure lua error, I don't think it is the result of me abusing love. I am probably just missing something obvious but I have been trying to figure out why this is for half an hour... I just need another set of eyes to find the error.
Help would be greatly appreciated.
In case you were wondering, this is a function to create scrolling backgrounds without polluting main.lua with messy code.
The code:
The error log:
Here is a link to the full .love file:
https://www.dropbox.com/s/solszghuslzqn ... .love?dl=1
Help would be greatly appreciated.
In case you were wondering, this is a function to create scrolling backgrounds without polluting main.lua with messy code.
The code:
Code: Select all
background = {}
function background:new(image,scrollSpeed)
local self = {}
self.image = image
self.a = {}
self.a.x = 0
self.a.y = 0
self.b = {}
self.b.x = 0
self.b.y = -self.image:getHeight()
self.scrollSpeed = scrollSpeed or 0
print(self) --SELF IS NOT NIL
function self:update(dt)
print(self) --SELF IS NOT NIL
self.a.y = self.a.y + (self.scrollSpeed * dt)
self.b.y = self.b.y + (self.scrollSpeed * dt)
if self.a.y > love.graphics.getHeight() then
self.a.y = -self.a.image:getHeight()
end
if self.b.y > love.graphics.getHeight() then
self.b.y = -self.b.image:getHeight()
end
end
print(self) --SELF IS NOT NIL
function self:draw()
print(self) --SELF IS NIL
love.graphics.draw(self.image, self.a.x, self.a.y)
love.graphics.draw(self.image, self.b.x, self.b.y)
end
return self
end
The error log:
Code: Select all
table: 0x12178488
table: 0x12178488
table: 0x12178488
nil
Error: background.lua:30: attempt to index local 'self' (a nil value)
stack traceback:
background.lua:30: in function 'draw'
main.lua:39: in function 'draw'
[string "boot.lua"]:467: in function <[string "boot.lua"]:435>
[C]: in function 'xpcall'
https://www.dropbox.com/s/solszghuslzqn ... .love?dl=1