Class init values are nil

,

Howdy, I know there's already an issue similar, but it did not solve my problem. I have these snippets of code:

class("Player").extends(gfx.sprite)
function Player:init(x, y)
    Player.super.init(self)
    self.x = x
    self.y = y
    self.vel_x = 0
    self.vel_y = 0
    self:setImage(image_player_idle)
    self:moveTo(x, y)
    self:setCollideRect(0, 0, 17, 11)
    self:add()
end
function Player:update()
    Player.super.update(self)

    self.x += self.vel_x
    self.y += self.vel_y
end
function Player:collisionResponse(other)
    self.vel_y = 0
    return "slide"
end
function pd.update()

    -- drawing bars
    for _, bar in ipairs(bars) do
        bar:update()
        bar.block:update()
    end

    player:update()

    -- health diplay
    if health >= 1 then
        image_heart:draw(178, 8)
    end
    if health >= 2 then
        image_heart:draw(193, 8)
    end
    if health == 3 then
        image_heart:draw(208, 8)
    end

end

for some odd reason when I run this I get this error:

Update error: main.lua:82: attempt to perform arithmetic on a nil value (field 'vel_x')
stack traceback:
    main.lua:82: in function <main.lua:79>
    [C]: in method 'update'

The thing that doesn't make sense to me, that x and y, the 2 variables set before vel_x and vel_y are not nil, they're their actual value, while vel_x and vel_y are nil. I have no clue why.

If anybody is wondering or just ended up here. It only works when I run gfx.sprite.update(), but not when I run the update functions seperately.

1 Like