Getting error "background.lua:3: attempt to index local 'self' (a nil value)," how to fix this?
Posted: Sun Mar 14, 2021 7:51 pm
I keep getting this error despite this same thing working in my other projects, what am I doing wrong?
Here is the code for reference:
And here is my main.lua file, just in case:
Here is the code for reference:
Code: Select all
local Background = {}
function Background:load()
self.image = love.graphics.newImage("assets/background.jpg")
self.x = 0
end
function Background:update(dt)
if love.mouse.getX() > 420 then
self.x = self.x - 4 * dt
elseif love.mouse.getX() < 60 then
self.x = self.x + 4 * dt
end
end
function Background:draw()
love.graphics.draw(self.image, self.x, 0, 0, 4, 4)
end
return Background
Code: Select all
local Background = require("background")
function love.load()
Background.load()
end
function love.update()
Background.update(dt)
end
function love.draw()
Background.draw()
end