@Qtipit Here’s an example I wrote for someone awhile ago. One of the direction buttons and the shoot button can be pressed at the same time.
function setup()
rectMode(CENTER)
dir=""
stab=""
btn={}
table.insert(btn,buttons(300,300,100,50,"UP"))
table.insert(btn,buttons(300,100,100,50,"DOWN"))
table.insert(btn,buttons(200,200,100,50,"LEFT"))
table.insert(btn,buttons(400,200,100,50,"RIGHT"))
table.insert(btn,buttons(300,400,100,50,"SHOOT"))
end
function draw()
background(40, 40, 50)
for a,b in pairs(btn) do
b:draw()
if b.pressed then
if b.text=="SHOOT" then
stab=b.text
elseif dir=="" then
dir=b.text
end
end
end
fill(255)
text(dir,WIDTH/2,HEIGHT-50)
text(stab,WIDTH/2,HEIGHT-100)
end
function touched(t)
for a,b in pairs(btn) do
b:touched(t)
end
end
buttons=class()
function buttons:init(x,y,w,h,txt)
self.x=x
self.y=y
self.w=w
self.h=h
self.text=txt
self.left=x-w/2
self.right=x+w/2
self.bottom=y-h/2
self.top=y+h/2
self.pressed=false
self.id=0
end
function buttons:draw()
fill(255)
if self.pressed then
fill(255, 158, 0, 255)
end
rect(self.x,self.y,self.w,self.h)
fill(255,0,0)
text(self.text,self.x,self.y)
end
function buttons:touched(t)
if t.state==BEGAN or t.state==MOVING and self.id==0 then
if t.x>self.left and t.x<self.right and
t.y>self.bottom and t.y<self.top then
self.pressed=true
self.id=t.id
end
end
if t.state==MOVING and self.id==t.id then
if t.x>self.left and t.x<self.right and
t.y>self.bottom and t.y<self.top then
self.pressed=true
else
self.pressed=false
self.id=0
dir=""
stab=""
end
end
if t.state==ENDED and self.id==t.id then
self.pressed=false
self.id=0
dir=""
stab=""
end
end