import "CoreLibs/sprites"
local pd <const> = playdate
local gfx <const> = pd.graphics
class('BaseSprite').extends(gfx.sprite)
function BaseSprite:init(x, y, image)
assert(image ~= nil, 'image is nil in BaseSprite')
self:setImage(image)
self:add()
self:moveTo(x, y)
end
It's inherited by InheritedSprite:
import "CoreLibs/sprites"
import "BaseSprite"
local pd <const> = playdate
local gfx <const> = pd.graphics
class('InheritedSprite').extends(BaseSprite)
function InheritedSprite:init(x, y)
local image = gfx.image.new(64, 64)
gfx.pushContext(image)
gfx.drawLine(0,0,64,64)
gfx.popContext(image)
assert(image ~= nil, 'image is nil in InheritedSprite')
self.super:init(x, y, image)
end
I've added two assert statements, confirming that image is not nil but I get the following error thrown:
Load failed, error: BaseSprite.lua:10: bad argument #-2 to 'setImage' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'setImage'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk
Update error: BaseSprite.lua:10: bad argument #-2 to 'setImage' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'setImage'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk
Maybe the bad argument to setImage isn’t the image but the sprite because you never called its init function. Try adding self.super.init(self) before self:setImage(image).
I've changed it to use InheritedSprite.super:init gives the same error.
import "CoreLibs/sprites"
import "BaseSprite"
local pd <const> = playdate
local gfx <const> = pd.graphics
class('InheritedSprite').extends(BaseSprite)
function InheritedSprite:init(x, y)
local image = gfx.image.new(64, 64)
gfx.pushContext(image)
gfx.drawLine(0,0,64,64)
gfx.popContext(image)
assert(image ~= nil, 'image is nil in InheritedSprite')
InheritedSprite.super:init(x, y, image)
end
Error:
Load failed, error: BaseSprite.lua:10: bad argument #-2 to 'setImage' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'setImage'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk
Update error: BaseSprite.lua:10: bad argument #-2 to 'setImage' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'setImage'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk
import "CoreLibs/sprites"
local pd <const> = playdate
local gfx <const> = pd.graphics
class('BaseSprite').extends(gfx.sprite)
function BaseSprite:init(x, y, image)
assert(image ~= nil, 'image is nil in BaseSprite')
self.super.init(self)
self:setImage(image)
self:add()
self:moveTo(x, y)
end
It does get past the self:setImage() now, but fails on the self:moveTo():
Load failed, error: CoreLibs/sprites.lua:38: bad argument #-2 to 'moveTo' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'moveTo'
CoreLibs/sprites.lua:38: in field 'init'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk
Update error: CoreLibs/sprites.lua:38: bad argument #-2 to 'moveTo' (playdate.graphics.spriteud expected, got nil)
stack traceback:
[C]: in method 'moveTo'
CoreLibs/sprites.lua:38: in field 'init'
BaseSprite.lua:10: in method 'init'
InheritedSprite.lua:16: in field 'init'
CoreLibs/object.lua:70: in global 'InheritedSprite'
main.lua:7: in main chunk