Search code examples
lualove2d

Love2D / .lua - attempt to call global (a nil value)


I hope you're doing well.

Could anyone help to find the issue with this error? I'm pretty sure 'PowerUpBall' is defined correctly.

this line is causing the error : src/states/PlayState.lua:39: attempt to call global 'PowerUpBall' (a nil value)

self.powerUpBall = PowerUpBall(self.ball.x,self.ball.y)

and 'PowerUpBall' is defined as this

PowerUpBall = Class{}

function PowerUpBall:init(x,y)
    self.x = x
    self.y = y
    self.dy = 15
    self.spawned = true
    self.achieved = false
end

function PowerUpBall:collides(target)
    if self.x + 16 > target.x and self.x < target.x + target.width and self.y > target.y and self.y < target.y + 16  then
        self.spawned = false
        self.achieved = true
    end
end

function PowerUpBall:update(dt)
    self.y = self.y + self.dy * dt
end


function PowerUpBall:render()
    if self.spawned == true then
        love.graphics.draw(gTextures['main'],gFrames['powerup'], self.x,self.y)
    end
end

I appreciate all comments

EDIT: This is related to an assignment in cs50G course


Solution

  • Do not forget to require the file containing PowerUpBall before using it!