Page 1 of 1

scope of self in classes

Posted: Wed Oct 21, 2020 1:56 am
by vinperdom
I'm using the classes lib from CS50 (I'll show in the code below). I have a Shield.lua class for a shield, https://github.com/vinperdom/Colorless/ ... Shield.lua (here, and the class lib is in lib/class.lua).
If I put all the logic inside the update function it works properly, but if I abstract a little with the "move" function, the quad never changes from what it was defined in init. Why?

Re: scope of self in classes

Posted: Wed Oct 21, 2020 4:43 pm
by pgimeno
Hi, welcome to the forums!

Code: Select all

function Shield:update(dt)
    if love.keyboard.isDown('w') then
        Shield:move('up')
    elseif love.keyboard.isDown('a') then
        Shield:move('left')
    elseif love.keyboard.isDown('s') then
        Shield:move('down')
    elseif love.keyboard.isDown('d') then
        Shield:move('right')
    end
end
You need to use self:move, not Shield:move. Not sure if that's the cause of your problem, though, because it's not very clear to me what you mean.