Hello all, I’m rather new to Codea and Lua and haven’t dabbled in programming for over a decade and am learning the ropes (or trying, anyway). Working my way through various tutorials and thinking up little exercises to try and test out skills, one runs into a paucity of input methods for user input as things are, so I said to myself: “let’s make a button!” For easy rapid input for choosing between options. I realize many have come before me and made button classes, but I wish to learn how to do this myself (conceptually speaking) and the buttons that others make are rather advanced for my current level. I have banged my head against this rather hard, and would love to have explained to me how people approach this problem that I’ve found. My problem is that the currentTouch variable stays in the ENDED state at its last touched location and I can’t figure out a good way to handle this. I’ve written a bit of code to demonstrate (that is obviously noob stuff - I’m a noob! It should certainly belong in a class also, but am just demonstrating).
-- buttons experiment
-- Use this function to perform your initial setup
function setup()
button = {}
button.x = WIDTH/2
button.y = HEIGHT/2
button.diameter = 200
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
font("AmericanTypewriter")
fontSize(40)
-- Do your drawing here
fill(88, 89, 108, 255)
ellipse(button.x, button.y, button.diameter)
fill(203, 78, 78, 255)
text("whatev", WIDTH/2, HEIGHT/2)
if IsPressed(button) then
print("hit")
end
end
function IsPressed(anyButton)
local left = anyButton.x - anyButton.diameter/2
local right = anyButton.x + anyButton.diameter/2
local bottom = anyButton.y - anyButton.diameter/2
local top = anyButton.y + anyButton.diameter/2
if CurrentTouch.x > left and CurrentTouch.x < right and
CurrentTouch.y > bottom and CurrentTouch.y < top
and CurrentTouch.state == ENDED
then
return true
else return false
end
end
Again, sorry for noobishness of this, just want to know a reasonably elegant way to handle it. I saw one person’s class that checked for the length of time that had passed in the ENDED state and I mostly got it, but just kind of wanted to see if that’s the way people really do this. Also, I apologize that there are ample examples included with the (rather complicated) codea programs like the sounds one, I just don’t fully grok that one either. Thanks!