Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Button function :)

edited July 2012 in Code Sharing Posts: 371

Hi everyone! I have been playing around with codea, and I just made a button function (Yes, that does say function. I didn't like the way that if you were using a class how you had to say Button:draw()). Here is my code:

function setup()
    rectMode(CENTER)
    button = function (x,y,w,h) return 0 end
end

function draw()
    background(0, 0, 0, 255)
    fill(0,255,0)
    if button(200,200,200,100) == true then
        background(255, 255, 255, 255)
    elseif button(200,200,200,100) == -1 then
        fill(0,200,0)
    end
    
    rect(200,200,200,100)
end

function touched(touch)
    function button(x,y,w,h)
        --[[ This is my attempt on making a button function,
            I thought about making it automatically draw a shape,
            but I could see how that could get messy very quickly.
            It is affected by the current rectMode(). ]]--
        if rectMode() == CORNER then
            w = x + w
            y = y + h
        elseif rectMode() == CENTER then
            x = x - w / 2
            y = y - h / 2
            w = x + w
            h = y + h
        elseif rectMode() == RADIUS then
            x = x - w
            y = y - h
            w = x + w * 2
            h = y + h * 2
        end
        if touch.x > x and 
        touch.x < w and
        touch.y > y and 
        touch.y < h then
            if touch.state == ENDED then
                return true
            else
                return -1
            end
        else
            return false
        end
    end
end
Tagged:
Sign In or Register to comment.