Hi everyone, this question is probably so simple to answer but i am a noob so cant fix it.
I am trying to make some code where when you press a rectangle a number goes up by 1.
Here is my code…
function setup()
d=0
parameter.watch("d")
end
function draw()
background(255, 255, 255, 255)
fontSize(60)
text(500+d,WIDTH/2,HEIGHT/2)
rect(WIDTH/2-300,HEIGHT/4,600,150)
end
function touch(t)
if t.x>WIDTH/2-300 and t.x<WIDTH/2+300 and t.y>HEIGHT/4 and t.y<HEIGHT/4+150 then
print("touch acknowledged")
d=d+1
end
end
function setup()
d=0
parameter.watch("d")
debounce = 0
end
function draw()
background(255, 255, 255, 255)
fontSize(60)
text(500+d,WIDTH/2,HEIGHT/2)
rect(WIDTH/2-300,HEIGHT/4,600,150)
if CurrentTouch.state == BEGAN and debounce == 0 then
touch(t)
debounce = 1
end
if CurrentTouch.state == ENDED then
debounce = 0
end
end
function touch(t)
if CurrentTouch.x>WIDTH/2-300 and CurrentTouch.x<WIDTH/2+300 and CurrentTouch.y>HEIGHT/4 and CurrentTouch.y<HEIGHT/4+150 then
print("touch acknowledged")
d=d+1
end
end
Instead of t use CurrentTouch, and to prevent the number shooting up if you keep your finger down, I created a debounce variable that will make it go up once per tap.
@Klupa56 Also, you should learn about the touch states of BEGAN, MOVING, and ENDED. Those are very important if you want to use the function touched(t) correctly.
@Mason Don’t give up completly on CurrentTouch. The function touched() and CurrentTouch both have their uses. For simple touch movement, CurrentTouch is fine. For more involved touch controls, the function touched() is a better choice. As I mentioned above, look up the use of the different states of the function touched().