I'm so stumped..

Hi guys. I was working a golf game interface, and the problem I’m encountering is very very strange. It only lets me click the first button that was drawn. It won’t let me click the other ones. It never plays the sound. But it draws all the buttons correctly. Here is the code:

supportedOrientations(LANDSCAPE_ANY)
function setup()
    displayMode(FULLSCREEN)
    buttons = {
        {
            X = 280;
            Y = HEIGHT / 2;
            locked = false;
        };
        {
            X = 390;
            Y = HEIGHT / 2;
            locked = true;
        };
        {
            X = 500;
            Y = HEIGHT / 2;
            locked = true;
        };
        {
            X = 610;
            Y = HEIGHT / 2;
            locked = true;
        };
    
    }
    atMenu = true
    tapVar = nil
end

function collision(x,y,xSize,ySize,touchX,touchY)
    if touchX > (x) and touchX < (x + xSize) and touchY > (y) and touchY < (y + ySize) then
        sound("Game Sounds One:Pop 2")
    end
end

function touched(tap)
    if tap.state == BEGAN then
        tapVar = tap
    elseif tap.state == ENDED then
        tapVar = nil
    end
end

function draw()
    background(255, 255, 255, 255)
    if atMenu then
        font("Futura-CondensedMedium")
        fontSize(36)
        fill(0, 0, 0, 255)
        text("Golf Days",WIDTH / 2,HEIGHT - 75)
        for i,v in pairs(buttons) do
            if v.locked then
                fill(255, 0, 9, 255)
            else
                fill(54, 255, 0, 255)
            end
            strokeWidth(4)
            stroke(0, 0, 0, 255)
            rect(v.X,v.Y,97,50)
            fontSize(16)
            font("Zapfino")
            fill(0, 0, 0, 255)
            text("Hole "..i,v.X + 46,v.Y + 20)
            if tapVar then
                collision(v.X,v.Y,97,50,tapVar.x,tapVar.y)
                tapVar = nil
            end
        end
    end
end

When posting code in the forum, put three tildes ~~~ at the top and bottom of the code block to make it display correctly

Apparently you don’t learn from your previous posts. 11 months ago you posted a question about button click detection where I showed you how to detect different things being touched. Everything you need to know has already been posted. If you’re not going to learn how to do things, then learn how to search for things. If you haven’t made any progress in a few days, ask again and I’ll show you again.

EDIT: If you truely don’t understand how the touched() function works, then I’ll give you an explanation.

Thanks I guess… But why is this code only working for the first button?

Ah, I figured it out. Just a very slim logical error on my part.