local pd <const> = playdate
local gfx <const> = pd.graphics
class("Foo").extends(gfx.sprite)
function Foo:init(size)
self.size = size
local image <const> = gfx.image.new(size, size)
gfx.pushContext(image)
gfx.drawCircleAtPoint(0,0,10) -- ERROR HERE
gfx.popContext()
self:setImage(image)
self:add()
end
Is throwing
Foo.lua:11: field 'drawCircleAtPoint' is not callable (a nil value)
stack traceback:
Foo.lua:11: in field 'init'
CoreLibs/object.lua:70: in global 'Foo'
main.lua:8: in main chunk
However, if i replace the draw circle funation with a draw rect one (gfx.drawRect(0, 0, 10, 10)) then things run fine.
This is super strange. Having given up, gone to bed and woken up it just works now.
local pd <const> = playdate
local gfx <const> = pd.graphics
class("Foo").extends(gfx.sprite)
function Foo:init(size, backgroundColor)
self.size = size
local image <const> = gfx.image.new(size, size, backgroundColor)
gfx.pushContext(image)
gfx.drawCircleAtPoint(self.size / 2, self.size / 2, self.size / 2)
gfx.drawRect(0, 0, self.size, self.size)
gfx.popContext()
self:setImage(image)
self:add()
end
function Foo:update()
end
However it's working without importing CoreLibs/graphics and without calling Foo.super.init(self).
I guess the missing call to super is Ok because it's parameterless?
No, you really need to do that other 'init' call, as you're extending the sprite class and without it your subclass instance isn't completely set up, 'infrastructure' wise (higher up in the hierarchy).