Text and touch

Whats the problem? When I run this, instead of getting the name of the color I just don’t get it. Yes again sorry for this really noobish problem. I’m sure the answer is right in front of face but I can’t figure it out

--main class
function setup()
    displayMode(FULLSCREEN)
    tx = WIDTH/2 --> this sets the x to halfway
    ty = HEIGHT/2 --> this sets the y to halfway
    c = color(255,255,0,255)
end

function draw()
    background(105, 151, 189, 255)
    fill(c) --> c is the colour we defined earlier
    ellipse(tx,ty,100)
    strokeWidth(5)
    textSize(1029)
    fontSize(75)
    font("SnellRoundhand-Black")
    textWrapWidth(846)
    roundRect(62,467,200,100,50)
    cy = "yello"
    colname(cy)
end

function colname(col)
        text("the colour is " .. col, WIDTH/2, 800)
end

function touched(touch)
    if touch.state == BEGAN then
        tx = touch.x
        ty = touch.y
        c = color(255, 249, 0, 255)
        cy = "yellow"
    end
    if touch.state == MOVING then 
        tx = touch.x
        ty = touch.y
        c = color(40, 212, 55, 255)
        cy = "green"
    end
    if touch.state == ENDED then
        tx = touch.x
        ty = touch.y
        c = color(245, 3, 3, 255)
        cy = "red"
    end
end
--roundRect class stolen from soundPlus
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

Never mind just did took the cy = “yellow” variable deceleration and put it outside the draw function now that that’s solved could somebody please tell me why that fixes it?

roundRect(62,467,200,100,50)
    -- cy = "yellow" < I took that from here
    colname(cy)
end
cy = "yellow" and put it here instead
function colname(col)
        text("the colour is " .. col, WIDTH/2, 800)
end

As you had it set up originally, cy was a local variable in touched and a different local variable in draw. So setting the value of one didn’t affect the other and the values were lost each time you left the respective functions.

The perils of scope.

I don’t think so, Mark, It wasn’t declared local anywhere so it would have been global. But in the draw it was reset to “yello” every time so never saw the change in touched.

Ok thanks @Andrew_Stacey now that that’s fixed is there a way to keep only the circle filled or is everything doomed to obey the first fill deceleration.

You can use noFill() to cancel the fill instruction (I think it is equivalent to fill(0,0,0,0) - ie fill with a transparent colour).