Hi!
I am making a 2 player pong game in order to get to grips with codea, I’m trying to get user input on the touch screen, but looking online and on the codea reference. All I see is that you can get the current touch coordinates and the touch coordinates from the last frame, is there a way to get the coordinates of two separate inputs at the same time(e.g. Two fingers on the touch screen at the same time)?
@rydergaz Here’s a simple example of moving 2 paddles. Slide your fingers in the grey areas.
displayMode(FULLSCREEN)
function setup()
rectMode(CENTER)
x1,y1,x2,y2=WIDTH/2,150,WIDTH/2,HEIGHT-150
end
function draw()
background(40, 40, 50)
fill(255,255,255,30)
rect(WIDTH/2,70,WIDTH,140)
rect(WIDTH/2,HEIGHT-70,WIDTH,140)
fill(255)
rect(x1,y1,100,20)
rect(x2,y2,100,20)
end
function touched(t)
if t.state==MOVING then
if t.y<150 then
x1=x1+t.deltaX
elseif t.y>HEIGHT-150 then
x2=x2+t.deltaX
end
end
end
@rydergaz If you want to use classes, then you would use the touch function inside the class. There is no correct way to program because it all depends on what you want to do and how you want to do it.
function setup()
print("This example tracks multiple touches and colors them based on their ID")
-- keep track of our touches in this table
touches = {}
end
-- This function gets called whenever a touch
-- begins or changes state
function touched(touch)
if touch.state == ENDED then
-- When any touch ends, remove it from
-- our table
touches[touch.id] = nil
else
-- If the touch is in any other state
-- (such as BEGAN) we add it to our
-- table
touches[touch.id] = touch
end
end
function draw()
background(0, 0, 0, 255)
for k,touch in pairs(touches) do
-- Use the touch id as the random seed
math.randomseed(touch.id)
-- This ensures the same fill color is used for the same id
fill(math.random(255),math.random(255),math.random(255))
-- Draw ellipse at touch position
ellipse(touch.x, touch.y, 100, 100)
-- Draw the id on the circle
fill(255)
text(touch.id, touch.x, touch.y+30)
end
end