Information Game

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

Hello, the way I do it is to basically make a variable (self.visible for me) set it to true to begin with, then at the top of draw and touched function in the button class put

function Button:draw()/ function Button:touched(touch)
if not self.visible then return end

Then you can add a function like this:

function Button:setVisible(bool)
 self.visible = bool
end

There you have element visibility.

i do the same

Ok I will try. Thx for advice!

Do i put it in pushstyle?

I have never used codea before :stuck_out_tongue:

Aaaaahhhhhhh

I want it to disappear when i click it

And then more button appears

But this just hides the button as soon as you press it

I-)

I mean press start in editor

Post the code here


#HERE

I don’t understand self.visible code.

@RandD - try this code. I have used tables to make your code much neater to maintain, see if you can follow all the changes


-- whoo
supportedOrientations(LANDSCAPE_RIGHT)
-- Use this function to perform your initial setup
function setup()
    --store names in table
    countries={"Japanese","Polish","American","Indian","Chinese"}
    
    local pos={vec2(w, HEIGHT/2),vec2(w, HEIGHT/4),vec2(w, HEIGHT/2.6),vec2(w, HEIGHT/6.9),vec2(w, HEIGHT/1.5)}
    buttons={}
    local w,h=WIDTH/2 - 150,HEIGHT*2/3
    
    for i=1,#countries do
        buttons[i]=Button(countries[i],vec2(w,h))
        h=h-80
    end
    --create back button, make it button 0
    buttons[0]=Button("Back",vec2(w,h))
    --store local data if not already done
    if readLocalData("info."..countries[1])==nil then saveData() end
end

function saveData()
    saveLocalData( "info.Japanese", "History... the history of Japan is very unique. History will cover The Emperor and the Samurai. First, the samurai. The first samurai claimed a majority of Japan in 1192." )
    saveLocalData( "info.Polish", "History..." )
    saveLocalData( "info.American", "History..." )
    saveLocalData( "info.Indian", "History..." )
    saveLocalData( "info.Chinese", "History..." )
end

-- This function gets called once every frame
function draw()
    background(220)
    if state==nil then
        for i=1,#buttons do
            buttons[i]:draw()
        end
    else buttons[0]:draw()
    end
end

function buttonPressed(n)
    print(countries[n])
    print(readLocalData("info."..n))
end

function touched(touch)
    if buttons[0]:touched(touch)==true then
        state=nil
    else
        for i=1,#buttons do
            if buttons[i]:touched(touch) then 
                state=i 
                output.clear()
                print(countries[state])
                print(readLocalData("info."..countries[state])) 
            end
        end
    end
end


Button = class()

function Button:init(displayName,p)
    -- you can accept and set parameters here
    self.displayName = displayName
    local w,h = textSize(self.displayName)
    self.pos = p
    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(30)

    -- 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)
    if touch.state == ENDED and
       self:hit(vec2(touch.x,touch.y)) then return true
    end
end

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

yay thank you

X)

:smiley: