I coded a simple game my daughter wrote and drew, and I’ve made a version that everybody can import, using only generic assets; zip file is attached.
The graphics don’t make any sense this way, but you can still play it and tell what’s happening.
It uses my old “super simple button” technique, and it all works very nicely, but I’ve been vexed by one thing.
The button function has x, y, width, and height parameters, and if width and/or height are nil, it checks a stored button table to see if there are saved values—so for instance, setting the width before doing drawing looks like this:
width = width or storedTable[buttonName].width
But when I try to do the same thing for the x and y values, the buttons stop working—for some reason adding those two ‘or’ statements breaks the game!
If you want to see what I mean, go to the uiPieces tab and reverse the commenting of lines 15 and 16, which are;
local x,y = buttonTable.x, buttonTable.y
--local x,y = x or buttonTable.x, y or buttonTable.y
@UberGoober to upload a zip, tap the little “File” button in the above compose message box. Then choose “Choose File” then select “Browse…” which will bring up the iOS file picker
@UberGoober The two or’s aren’t ruining everything. You just have bad values for the button positions. Make the following changes for b1 and b2 in the code and you’ll see a red and green circle where the center of your button should be.
In Main, draw()
function draw()
profiler.draw()
currentScreen()
pushStyle()
fill(255,0,0)
ellipse(b1.x,b1.y,50)
fill(0,255,0)
ellipse(b2.x,b2.y,50)
popStyle()
end
In uiPieces, button
--set button drawing values, using saved values if none passed in
local buttonTable = uiPieceHandler.buttons[name]
--local x,y = buttonTable.x, buttonTable.y
local x,y = x or buttonTable.x, y or buttonTable.y
b1=vec2(x,y)
b2=vec2(buttonTable.x,buttonTable.y)
width = width or buttonTable.width
height = height or buttonTable.height
That actually wasn’t unexpected behavior, since the code was supposed to use x and y only if those values were actually passed in as parameters, and otherwise fall back to the stored values, so the stored values and the passed-in values should draw at different places.
…or should they?
You made me re-think that. Now I’m capturing any passed-in x and y values, and overwriting the stored values with them, and it works!
Thank you so much.
When I clean it up I’ll post a fixed .zip in case anyone wants to see it.