Hi,
I’m totally new to Codea and until now I only learned markup languages. So I’m trying to get into lua and followed the tutorials in the wiki (wish there’d be more!). I’m trying to create a UI first, as I think it would be the most easy part. I fully understand the mesh concept, but am stuck with TouchEvents. Right now, I would like to initiate an action when pushing a button (actually rectangle meshes with texture).
Here’s the code I’ve come up with:
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
--Set the viewer to fullscreen
displayMode( FULLSCREEN )
c1 = color(31, 57, 84, 255)
c2 = color(36, 115, 196, 255)
by = 50
ty = 100
iw = 120
i2w = 100
ih = 30
buttonframe = Frame(865, 50, WIDTH - 5, HEIGHT - 55)
topbar = mesh()
id1 = topbar:addRect(WIDTH/2,HEIGHT-ty/2, WIDTH,ty)
topbar:setRectColor(id1, 218, 75, 75, 255)
bottombar = mesh()
bottombar.vertices = {vec2(0,0), vec2(WIDTH,0), vec2(0,by), vec2(0,by), vec2(WIDTH,by), vec2(WIDTH,0)}
bottombar.colors = {c1, c1, c2, c2, c2, c1}
menu_img = readImage("Cargo Bot:Menu Button")
icone = mesh()
icone.texture = menu_img
icone:addRect(iw/2+20, by/2, iw, ih)
icone:setRectTex(1,0,0,1,1)
exit_img = readImage("Cargo Bot:Clear Button")
ico_exit = mesh()
ico_exit.texture = exit_img
ico_exit:addRect(WIDTH - i2w/2 - 20, by/2, i2w, ih)
ico_exit:setRectTex(1,0,0,1,1)
end
function touched(touch)
sound(SOUND_HIT, 33733)
if ico_exit:touched(touch) then
close()
end
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)
-- Do your drawing here
topbar:draw()
bottombar:draw()
icone:draw()
ico_exit:draw()
end
Obviously, TouchEvents are not that easy! I understood that there are two methods discussed on this forum: physics with collide events and touch events depending on coordinates. The later seem to be not so resource friendly as I understood, if there are a lot of tappable shapes/buttons.
I would like to know what you think would be the best practice in my situation to handle touch events. And am I in the right direction for the UI design (using meshes, etc…).
Thanks a lot