Attempt to perform arithmetic on "color" a nil value.
Posted: Fri May 29, 2020 6:02 pm
Have a Brick class that declares self.color = 1 in its init function, and however when I call self.color in the Brick's render function, I get the error that color is a nil value. The code works perfectly fine if I replace self.color in Brick:render() with a 1, and also if I put self.color = 1 in the beginning of the render function. I'd like to know what's up, and I believe I read somewhere that self variables were automatically global?
Thanks
Code: Select all
function Brick:init(x, y)
-- used for coloring and score calculation
self.tier = 0
self.color = 1
self.x = x
self.y = y
self.width = 32
self.height = 16
-- used to determine whether this brick should be rendered
self.inPlay = true
end
function Brick:render()
if self.inPlay then
love.graphics.draw(gTextures['main'],
-- multiply color by 4 (-1) to get our color offset, then add tier to that
-- to draw the correct tier and color brick onto the screen
gFrames['bricks'][1 + ((self.color - 1) * 4) + self.tier],
self.x, self.y)
end
end