"self" problem

Probably my missing something, but in the following code (simplified to show the problem)

Block = class()

function Block:init(x,y)
    -- you can accept and set parameters here
  self.x = x
  self.y = y
  self.model = "Planet Cute:Gem Blue"
end

function Block:draw()
    -- Codea does not automatically call this method
  sprite("Planet Cute:Gem Blue", self.x, 100)
end

function Block:touched(touch)
    -- Codea does not automatically call this method
end

the “self.x” usage in sprite generates an error – “attempt to index local ‘self’ (a nil value)”

Any idea why? What I’m missing?

My main.lua is

function setup()
	block = Block(200,200)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
block.draw()
    
end

Thanks

You must use block:draw(), I remember @RonJeffries running into this problem too…
EDIT:
This should help here

Thanks. That solved it I assume the use of “:” applies to all class function calls, right?

Hello @akiva. The short answer to your last question is yes. The function definition:


function Block:draw()
    ...
end

is equivalent to:


function Block.draw(self)
    ...
end

and so you must either call the function you have defined with block:draw() or, equivalently, with block.draw(block).

Thanks @mpilgrem - that makes it much clearer.