@Vile Your next question is probably going to be how to do multiple buttons. I don’t know your programming skills, but this example uses a class
to handle multiple buttons.
function setup()
rectMode(CENTER) -- use x,y position for rect center
b1=button(200,200,100,50,color(0,255,0),"button 1",color(255))
b2=button(200,300,100,50,color(255,255,0),"button 2",color(255,0,250))
b3=button(200,400,100,50,color(255,0,255),"button 3",color(0,0,255))
end
function draw()
background(0)
b1:draw()
b2:draw()
b3:draw()
end
function touched(t)
b1:touched(t)
b2:touched(t)
b3:touched(t)
end
button=class()
function button:init(x,y,w,h,bc,t,tc)
self.x=x
self.y=y
self.width=w -- button width
self.height=h -- button height
self.btColor=bc -- button color
self.text=t -- text
self.txColor=tc -- text color
end
function button:draw()
fill(self.btColor)
rect(self.x,self.y,self.width,self.height)
fill(self.txColor)
text(self.text,self.x,self.y)
end
function button:touched(t)
if t.state==BEGAN then
if t.x>self.x-self.width/2 and t.x<self.x+self.width/3 and
t.y>self.y-self.height/2 and t.y<self.y+self.height/2 then
if self.text=="ON" then
self.text="OFF"
self.btColor=color(0,255,0)
else
self.text="ON"
self.btColor=color(255,0,0)
end
end
end
end