Is there any reason why a polygon or shape will not render on the screen through an [class]:update versus the standard playdate.update?
function playdate:update()
gfx.fillPolygon(20,148, 70,148, -660,240)
playdate.timer.updateTimers()
gfx.sprite.update()
end
class('GameScene').extends(gfx.sprite)
function GameScene:init()
self:drawBG()
self:drawForeground()
self:drawPlayer()
end
function GameScene:update()
gfx.fillPolygon(20,148, 70,148, -660,240)
end
Instead of :update() you will need to do your drawing in a function GameScene:draw() callback. Use :update() do do other non-drawing things, such as adjusting the position of your sprite.
You'll also need to make sure you create an instance of the sprite and add it, if you're not already. Something like:
local gameSceneSprite = GameScene()
gameSceneSprite:add()
It looks like you're also trying to do some drawing in the :init() function, which will not work - that stuff should also probably go in your :draw() function (it's hard to say for sure without seeing the rest of the code, of course).
One other note: your main update function should be playdate.update() rather than playdate:update() (period rather than colon). The fillPolygon() call there will likely not be visible due to the later sprite.udpate() call, which first clears the dirty areas of the screen with your background color, which is white by default.
Thanks for the guidance and tips! I have those functions in the :init function to draw them on scene load. Otherwise, they draw in a previous scene.
The draw callback never seems to work for me (no print information for either :update or :draw):
class('GameScene').extends(gfx.sprite)
function GameScene:init()
self:drawBG() -- draw these items on scene load
self:drawForeground()
self:drawPlayer()
end
function GameScene:update()
print("test update")
end
function GameScene:draw()
print("test draw")
gfx.fillPolygon(20,148, 70,148, -660,240)
gfx.fillPolygon(90,148, 100,148, -250, 240, -350,240)
gfx.fillPolygon(100,148, 120,148, -100, 240, -180,240 )
gfx.fillPolygon(325,140, 380,140, 500,160)
end
One thing I'm noticing is that in order for the GameScene:update to work, I need to have at least one :add() in the GameScene:init. Now, I would think that having a self:draw() would help with making GameScene:draw() work, but I think it should run without directly calling it, correct?
The :add() call does not need to be in your :init() function, but you will need to call :add() somewhere on any sprite you wish to appear in your game.
Are you calling playdate.graphics.sprite.update() from playdate.update()? That call will in turn call the sprite's :update() and :draw() methods, the latter of which will only be called if the sprite has been marked as dirty either by the system or directly by you.
Note that you will also need to set a size for your sprite before your custom :draw() method will be called, either by calling :setSize() or :setBounds().
The coordinates for drawing that you do in the custom draw method will be relative to your sprite's bounds, rather than screen coordinates.