Hai! I’m new to Lua Language. I wanted to create two basic touchscreen controller for my game in Codea, joystick and buttons. But my code below seems to have a problem, I can’t create multiple Joysticks or multiple Buttons with the same touch variable (I wanted a separate control but every time I move the first joystick, it will move both). I learn how to create a table for multiple touches from the example, but how do I assigned different ID to different button every time it pressed/touched? Can someone help me?
My background is from Processing and Openframeworks
--# Main
-- Game_Control_UI
function setup()
smooth() --enable smooth graohic
displayMode(FULLSCREEN) --set wether the display should be fullscreen
joy = Joystick(WIDTH/4, HEIGHT/2, 200)
joy2 = Joystick(WIDTH/4*2, HEIGHT/2, 200)
end
function draw()
background(55) --fill the screen with dark grey color
joy:draw()
joy2:draw()
end
function touched(touch)
joy:touched(touch)
joy2:touched(touch)
end
——————————————————————————————————————
Joystick = class()
function Joystick:init(posX, posY, size)
self.x = posX --position X
self.y = posY --position Y
self.s = size --joystick size
self.minX = posX - size/2 --left edge
self.maxX = posX + size/2 --right edge
self.minY = posY - size/2 --bottom edge
self.maxY = posY + size/2 --top edge
end
function Joystick:draw()
posX = self.x
posY = self.y
size = self.s
minX = self.minX
maxX = self.maxX
minY = self.minY
maxY = self.maxY
fill(200, 55)
ellipse(posX, posY, size)
if inBound == true then
if tX > minX and tX < maxX then
joyX = (tX+posX)/2
elseif tX < minX then joyX = (minX+posX)/2
elseif tX > maxX then joyX = (maxX+posX)/2
end
if tY > minY and tY < maxY then
joyY = (tY+posY)/2
elseif tY < minY then joyY = (minY+posY)/2
elseif tY > maxY then joyY = (maxY+posY)/2
end
fill(255)
else
fill(155)
joyX = posX
joyY = posY
end
ellipse(joyX, joyY, size/1.2)
end
function Joystick:touched(touch)
minX = self.minX
maxX = self.maxX
minY = self.minY
maxY = self.maxY
tX = touch.x --rename variable touchX
tY = touch.y --rename variable touchY
if touch.state == BEGAN and tX > minX and tX < maxX and tY > minY and tY < maxY then
inBound = true
elseif touch.state == ENDED or touch.state == BEGAN and tX < minX and tX > maxX and tY < minY and tY > maxX then
inBound = false
end
end