Need help with currentTouch

function setup()
    displayMode(FULLSCREEN)
    X=350
    Y=250
    W=300
    H=200
    score=0
end
function draw()
    background(150,150,150,255)
    fill(255)
    font("Optima-Bold")
    fontSize(50)
    textWrapWidth(500)
    text(score,500,600)
end
fill(255,0,0,200)
if CurrentTouch.x>350 and CurrentTouch.x<650 and CurrentTouch.y>250 and CurrentTouch.y<450 then
fill(200,0,0,200) else fill(255,0,0,200)
end
stroke(255)
strokeWidth(4)
rect(X,Y,W,H)
end

I need help with this code. The code only runs when i remove the rectangle. Can someone explain why please?

@wildcat_JK, you were calling end before drawing, here’s a sample, i’m not sure if it’ll fit what you’re looking for though;

function setup()
    X = 350
    Y = 250
    W,H = 300,200
    Score = 0
    fillCol = color(0,0,0,0)
end

function draw()
    background(150,150,150,255)
    fill(255)
    font("Optima-Bold")
    fontSize(50)
    textWrapWidth(500)
    text(Score,500,600)
    if CurrentTouch.x >= WIDTH/2 then
        fillCol = color(255,0,0,255)
        else
        fillCol = color(0,255,0,255)
    end
    fill(fillCol)
    rect(X,Y,W,H)
end

here is my contriburion, i suggest you to use the touched(t) function, currentTouch is not so good.

function setup()
    displayMode(FULLSCREEN)
    X=350
    Y=250
    W=300
    H=200
    C1 = color(255,0,0,200)
    C2 = color(200,0,0,200)
    C = C1
    score=0
end
function draw()
    background(150,150,150,255)
    fill(255)
    font("Optima-Bold")
    fontSize(50)
    textWrapWidth(500)
    text(score,500,600)
    fill(C)
    stroke(255)
    strokeWidth(4)
    rect(X,Y,W,H)
end
function touched(t)
    if t.x>350 and t.x<650 and t.y>250 and t.y<450 then
        C = C2
    end
    if t.state == ENDED then
        C = C1
    end
end


PS: i cant belive 2 people answered while i was preparing my answer… This was only 10 min!

@CodeaNoob thanks!! I kept trying to make the code work but it didn’t so… Thanks :slight_smile:

@wildcat_JK The only thing wrong with your code was an extra end statement. Here’s your code with the statement commented out.

EDIT: You’ll also find out that there are a lot of different ways to do what you’re trying to do here.


function setup()
    displayMode(FULLSCREEN)
    X=350
    Y=250
    W=300
    H=200
    score=0
end

function draw()
    background(150,150,150,255)
    fill(255)
    font("Optima-Bold")
    fontSize(50)
    textWrapWidth(500)
    text(score,500,600)
--end
    fill(255,0,0,200)
    if CurrentTouch.x>350 and CurrentTouch.x<650 and CurrentTouch.y>250 and CurrentTouch.y<450 then
    fill(200,0,0,200) else fill(255,0,0,200)
    end
    stroke(255)
    strokeWidth(4)
    rect(X,Y,W,H)
end

@Jmv38 thanks! :slight_smile:

@dave1707 thanks for helping me :smiley:

@wildcat_JK - this explanation may help you

https://coolcodea.wordpress.com/2014/12/28/188-understanding-touch/