There's probably a better way to do this, but I can't get a live timer that display how long since the game started. Here's what I'm trying
function createTimeDisplay()
timeSprite = gfx.sprite.new()
updateTimeDisplay()
timeSprite:setCenter(0, 0)
timeSprite:moveTo(170, 4)
timeSprite:add()
end
function updateTimeDisplay()
local elapsed_time = pd.getElapsedTime()
local minutes = math.floor(elapsed_time / 60)
local seconds = math.floor(elapsed_time % 60)
gfx.setFont(uiFont)
local timeText = string.format("Time: %d:%02d", minutes, seconds)
local textWidth, textHeight = gfx.getTextSize(timeText)
local timeImage = gfx.image.new(textWidth, textHeight)
gfx.pushContext(timeImage)
gfx.drawText(timeText, 0, 0)
gfx.popContext()
timeSprite:setImage(timeImage)
end
function pd.update()
gfx.sprite.update()
pd.timer.updateTimers()
if not gameOver then
updateTimeDisplay()
end
end
createTimeDisplay()
The timer at the top displays 0:00 and never updates when I thought it would be called in the update loop. I can see that updateTimeDisplay is getting called. Any thoughts?
Hello, thats weird, I've copied and tested your code and it works correctly (time updates). Only thing I didn't copy is the gameOver condition. Are you sure gameOver is really false on your end?
import "CoreLibs/graphics"
import "CoreLibs/sprites"
local pd <const> = playdate
local pd_graphics <const> = pd.graphics
local pd_sprite <const> = pd_graphics.sprite
function createTimeDisplay()
timeSprite = pd_graphics.sprite.new()
updateTimeDisplay()
timeSprite:setCenter(0, 0)
timeSprite:moveTo(170, 4)
timeSprite:add()
end
function updateTimeDisplay()
local elapsed_time = pd.getElapsedTime()
local minutes = math.floor(elapsed_time / 60)
local seconds = math.floor(elapsed_time % 60)
pd_graphics.setFont(uiFont)
local timeText = string.format("Time: %d:%02d", minutes, seconds)
local textWidth, textHeight = pd_graphics.getTextSize(timeText)
local timeImage = pd_graphics.image.new(textWidth, textHeight)
pd_graphics.pushContext(timeImage)
pd_graphics.drawText(timeText, 0, 0)
pd_graphics.popContext()
timeSprite:setImage(timeImage)
end
createTimeDisplay()
function pd.update()
pd_sprite.update()
updateTimeDisplay()
end
Hmm weird indeed. gameOver is definitely false. I wonder if I'm somehow altering the timer elsewhere, and the actual value is 0:00. I'll have to look into that!