Reputation: 11
I am working on a powerup class for a project and when I try to run it I get an error saying "attempted to index "self" a nil value. I would really appreciate if somebody helped me. Thanks for reading!
PS: Yes, I am using colons and not dots for my Render, Init, Update functions.
function Powerup:init()
-- simple positional and dimensional variables
self.x = x
self.y = y
self.width = 11
self.height = 11
self.dy = 0
self.dx = 0
self.inPlay = true
end
--[[
Expects an argument with a bounding box, be that a paddle or a brick,
and returns true if the bounding boxes of this and the argument overlap.
]]
function Powerup:collides(target)
if self.x > target.x + target.width or target.x > self.x + self.width then
return false
end
if self.y > target.y + target.height or target.y > self.y + self.height then
return false
end
return true
end
function Powerup.trigger(paddle)
if self.inPlay then
self.inPlay = false
end
end
function Powerup:update(dt)
self.x = self.x
self.y = self.y + self.dy * dt
if self.y <= 0 then
self.y = 0
self.dy = -self.dy
gSounds['wall-hit']:play()
end
end
function Powerup:render()
if self.inPlay then
love.graphics.draw('zucc.png', self.x, self.y)
end
end```
Upvotes: 1
Views: 192
Reputation: 616
table.method(self)
is equal to table:method()
, both are given self
as the first parameter, which means that if you are using the colon :
, you do not need to declare self
as a parameter.
If that doesn't solve your problem, could you be more detailed and tell us exactly which line is calling for the error?
EDIT:
the reason the error is being caused is because you did not declare the variable x
and y
as parameters. The error is saying that "you are trying to insert the index x
of self
the value x
, but the variable X does not exist, that is, you are trying to insert nil
into the variable"
you can solve this problem by declaring the parameters in the function:
Powerup:init(x, y)
and then call the function, giving the values x
and y
, example:
Powerup:init(3,5)
now self.x
is 3, and self.y
is 5
Upvotes: 1