I was running through the examples in the SDK and came across a bug with a simple fix.
in arcs.lua pressing A or B inits the 'outterRadius' and 'innerRadius.' if u press any button of the d-pad before pressing A or B, the variables have not been initialized yet and the game will crash.
There three fixes ive thought of.
-
init those values along with 'radius' at the top of the file. seems to work with these declared globally or locally, im curious what the optimal solution is there.
-
in the for loop of the game update function, local values with the same names are initialized (innerRadius & outterRadius). if you make them global and not local, this also fixes it.
-
don't let d-pad input do anything unless A or B has been pressed. i don't really like this solution, but it does fix the problem.
let me know what you think, or if the bug is not reproducible and for some reason is only on my machine (mac m1)
here's code with local delcarations at the top, and with 'local' removed from the loop in update()
import 'CoreLibs/graphics'
gfx = playdate.graphics
gfx.clear()
startAngle = 0
ds = 0.4
endAngle = 0
de = 2
radius = 120
local innerRadius = math.random(120)
local outerRadius = math.random(120)
nrings = 12
gfx.setStrokeLocation(gfx.kStrokeOutside)
function playdate.update()
gfx.clear()
gfx.setColor(gfx.kColorWhite);
gfx.drawRect(200 - radius, 120 - radius, radius * 2 + 1, radius * 2 + 1)
startAngle = startAngle + ds
endAngle = endAngle + de
gfx.setColor(gfx.kColorBlack);
for i=1,nrings-1
do
innerRadius = i * radius/nrings
outerRadius = (i+1) * radius/nrings
gfx.setLineWidth(outerRadius - innerRadius)
gfx.drawArc(200, 120, innerRadius, (nrings - i) * startAngle, (nrings - i) * endAngle)
end
end
function drawarc()
gfx.setColor(gfx.kColorWhite);
gfx.drawRect(0, 0, 400, 240)
gfx.setColor(gfx.kColorXOR)
gfx.setLineWidth(outerRadius - innerRadius)
gfx.drawArc(200, 120, innerRadius, startAngle, endAngle)
playdate.display.flush()
end
function playdate.upButtonDown()
startAngle = startAngle + 10
drawarc()
end
function playdate.downButtonDown()
startAngle = startAngle - 10
drawarc()
end
function playdate.leftButtonDown()
endAngle = endAngle - 10
drawarc()
end
function playdate.rightButtonDown()
endAngle = endAngle + 10
drawarc()
end
function playdate.AButtonDown()
innerRadius = math.random(120)
outerRadius = math.random(120)
drawarc()
end
function playdate.BButtonDown()
innerRadius = 50
outerRadius = 100
drawarc()
end