Hi, I got codea and have buttons and roundrects. The only thing I need is hiding the buttons. So far my main code is
-- whoo
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
button = Button("Japanese")
button.action = function() buttonPressed() end
button2 = Button("Polish")
button2.action = function() button2Pressed() end
button3 = Button("American")
button3.action = function() button3Pressed() end
button4 = Button("Indian")
button4.action = function() button4Pressed() end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(92, 87, 87, 118)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
drawButton()
end
function drawButton()
button.pos = vec2(WIDTH/2 - 150, HEIGHT/2)
button:draw()
button2.pos = vec2(WIDTH/2 - 150, HEIGHT/4)
button2:draw()
button3.pos = vec2(WIDTH/2 - 150, HEIGHT/2.6)
button3:draw()
button4.pos = vec2(WIDTH/2 - 150, HEIGHT/6.9)
button4:draw()
end
function buttonPressed()
print("Japan")
end
function button2Pressed()
print("Poland")
end
function button3Pressed()
print("America")
end
function button4Pressed()
print("India")
end
function touched(touch)
button:touched(touch)
button2:touched(touch)
button3:touched(touch)
button4:touched(touch)
end
I pressed the run button and tried it X_X and it works.
But how should i hideall when i press a button, then have more buttons?
Oh yeah, my button and roundrect code
Button
Button = class()
function Button:init(displayName)
-- you can accept and set parameters here
self.displayName = displayName
self.pos = vec2(0,0)
self.size = vec2(0,0)
self.action = nil
self.color = color(0, 70, 255, 184)
end
function Button:draw()
-- Codea does not automatically call this method
pushStyle()
fill(self.color)
font("MarkerFelt-Thin")
fontSize(22)
-- use longest sound name for size
local w,h = textSize(self.displayName)
w = w + 20
h = h + 30
roundRect(self.pos.x - w/2,
self.pos.y - h/2,
w,h,30)
self.size = vec2(w,h)
textMode(CENTER)
fill(54, 65, 96, 255)
text(self.displayName,self.pos.x+2,self.pos.y-2)
fill(0, 255, 3, 255)
text(self.displayName,self.pos.x,self.pos.y)
popStyle()
end
function Button:hit(p)
local l = self.pos.x - self.size.x/2
local r = self.pos.x + self.size.x/2
local t = self.pos.y + self.size.y/2
local b = self.pos.y - self.size.y/2
if p.x > l and p.x < r and
p.y > b and p.y < t then
return true
end
return false
end
function Button:touched(touch)
-- Codea does not automatically call this method
if touch.state == ENDED and
self:hit(vec2(touch.x,touch.y)) then
if self.action then
self.action()
end
end
end
roundRect
function roundRect(x,y,w,h,r)
pushStyle()
insetPos = vec2(x+r,y+r)
insetSize = vec2(w-2*r,h-2*r)
--Copy fill into stroke
local red,green,blue,a = fill()
stroke(red,green,blue,a)
noSmooth()
rectMode(CORNER)
rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
if r > 0 then
smooth()
lineCapMode(ROUND)
strokeWidth(r*2)
line(insetPos.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y)
line(insetPos.x, insetPos.y,
insetPos.x, insetPos.y + insetSize.y)
line(insetPos.x, insetPos.y + insetSize.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
line(insetPos.x + insetSize.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
end
popStyle()
end