Basic question why is my text not displaying

I am back to some PD dev and always seem to get stuck remembering what I am doing why is my text not staying on screen -

-- main.lua

-- Import the playdate library
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

-- Define shorthand for commonly used libraries
local gfx = playdate.graphics

-- Set up game variables
local message = "Title of Game"
local font = gfx.getSystemFont()
local screenWidth, screenHeight = playdate.display.getSize()

-- Create a rectangle sprite
local rectSprite = gfx.sprite.new()
local rectWidth, rectHeight = 50, 30
rectSprite:setSize(rectWidth, rectHeight)

-- Adjusted position
rectSprite:moveTo(rectWidth / 2, screenHeight / 2 + 50)
rectSprite:add()

-- Set up the sprite's drawing function
function rectSprite:draw()
    gfx.setColor(gfx.kColorBlack)
    gfx.fillRect(0, 0, rectWidth, rectHeight)
end

-- Update function, called once per frame
function playdate.update()
    -- Clear the screen
    gfx.clear()

    gfx.setFont(font)
    gfx.drawTextAligned(message, screenWidth / 2, screenHeight / 4, kTextAlignment.center)

    -- Move the rectangle sprite across the screen
    local x, y = rectSprite:getPosition()
    x = x + 2              -- Move the sprite 2 pixels to the right each frame
    if x > screenWidth + rectWidth / 2 then
        x = -rectWidth / 2 -- Reset the position to the left edge once it goes off-screen
    end
    rectSprite:moveTo(x, y)

    -- Update sprites
    gfx.sprite.update()

    -- Update timers
    playdate.timer.updateTimers()
end

Not exactly sure where the issue is, my guess is that the gfx.sprite.update() also clears the screen (or rather dirty rects), but thats just a guess. If you replace the gfx.clear() with gfx.sprite.update(), the title is gonna show. Or keep the gfx.clear() and place the gfx.sprite.update() directly after it. Point is to have the gfx.sprite.update() on top of the playdate.update scope.

1 Like

Thanks moved the update to be after clear