Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Multitouch mess

edited February 2014 Posts: 105

It seems that I cant understand multitouch. Here is my touch function with all the mess that is included:

``````function Player:touched(touch)
--Move
if touch.state==BEGAN then
self.touches[touch.id]=touch
end
for k,touchs in pairs(self.touches) do
if touchs.state==MOVING and touchs.x>0 and touchs.x<150 and
touchs.y>0 and touchs.y<150 then
self.speed=touchs.y/9
end
--Look x and y
if touchs.state==BEGAN then
self.dax=touchs.x
self.lyx=touchs.y
end
if touchs.state==MOVING and touchs.x>150 then
self.da=self.dalx-(touchs.x-self.dax)
self.angle=self.da/80
self.tly=self.lylx-(self.lyx-touchs.y)
self.tly=self.tly/100
if self.tly>1.5 then self.tly=1.5 end
if self.tly<-1.5 then self.tly=-1.5 end
end
if touchs.state==ENDED then
self.touch[touchs.id]=nil
self.speed=0
self.dax=0
self.dalx=self.da
self.lylx=self.tly
self.lyx=0
end
end
end
``````

Help would be appreciated!

EDIT: Fixed BEGAN and MOVING

Tagged:

• Mod
Posts: 3,297

it is not clear what you are trying to do. Difficult then to help you?

• Posts: 105

Oh, didnt mean to be unclear. What I want to acheive is to get the multitouch in the code to work, as it didnt when I tried it before posting this question. There is a problem somewhere, but I cant find it...

• Mod
edited February 2014 Posts: 3,297

oh really? You want it to work? Silly me, i thought you wanted it not to work!
From the indication you've given me up to now, my most appropiate proposal would be: 'make it work then, by correcting the bugs'. No, dont thank me, it is natural to help when i can.
Now, if you can be a bit more specific on describing what result you want exactly, maybe i will be able to be more specific too.
;-)

• Posts: 105

Haha, well I might not always be so clear.
However, what I want is the user to have one finger touching/adjusting the speed and another finger to look around. After figuring out the basics of multitouch I executed my code but the game didn't respond. So thats the problem that I am incapable of solving.

• Mod
Posts: 8,739

@MMGames One thing I noticed was these "if" statements. touchs.state can't be BEGAN and MOVING at the same time, so MOVING will never be true.

``````    if touchs.state==BEGAN then
if touchs.state==MOVING and touchs.x>0 and touchs.x<150 and
``````
• edited February 2014 Posts: 105

Fixed. Thanks @dave1707
But the problem is still there...

• Posts: 198

Do you have codea community? Have a look at Spritea or Spacewar in there, or if not look here:

http://twolivesleft.com/Codea/Talk/discussion/4487/spritea-a-rapid-scene-generation-tool-from-sprites-located-in-the-dropbox-folder/p1

and here:

http://twolivesleft.com/Codea/Talk/discussion/4585/spacewar-soon-to-be-on-the-app-store/p1

Both use multitouch and support up to 11 touches.

If you wanted to post your whole code for us to look at, we might be able to help out more.

• Posts: 2,161

I suspect that your code is getting called too often. Split it into

``````function Player:touched(touch)
--Move
if touch.state==BEGAN then
self.touches[touch.id]=touch
end
end

function Player:processTouches()
for k,touchs in pairs(self.touches) do
if touchs.state==MOVING and touchs.x>0 and touchs.x<150 and
touchs.y>0 and touchs.y<150 then
self.speed=touchs.y/9
end
--Look x and y
if touchs.state==BEGAN then
self.dax=touchs.x
self.lyx=touchs.y
end
if touchs.state==MOVING and touchs.x>150 then
self.da=self.dalx-(touchs.x-self.dax)
self.angle=self.da/80
self.tly=self.lylx-(self.lyx-touchs.y)
self.tly=self.tly/100
if self.tly>1.5 then self.tly=1.5 end
if self.tly<-1.5 then self.tly=-1.5 end
end
if touchs.state==ENDED then
self.touch[touchs.id]=nil
self.speed=0
self.dax=0
self.dalx=self.da
self.lylx=self.tly
self.lyx=0
end
end
end
``````

Then call processTouches at most once each cycle.

• Mod
edited February 2014 Posts: 8,739

@MMGames From what I got out of your questions, you want 2 squares that will control 2 different things. Here is a small example with 2 squares. Since I don't know exactly what you want them to do, I just displayed the left and right x,y values as you move your fingers around the squares. You can take the touched function and add whatever code you want each square to do.

``````displayMode(FULLSCREEN)

function setup()
s1=vec2(0,0)    -- x,y left square
s2=vec2(0,0)    -- x,y right square
end

function draw()
background(40, 40, 50)
fill(255)
rect(100,100,150,150)    -- display left square
rect(WIDTH-250,100,150,150)    -- display right square
text(s1.x.."  "..s1.y,200,HEIGHT/2)    -- show left x,y values
text(s2.x.."  "..s2.y,WIDTH-200,HEIGHT/2)    -- show right x,y values
end

function touched(t)
if t.state==BEGAN or t.state==MOVING then
-- check left square
if t.x>100 and t.x<250 and t.y>100 and t.y<250 then
-- do whatever when the left square is touched
s1.x=t.x    -- left square x,y positions
s1.y=t.y
end
-- chack right square
if t.x>WIDTH-250 and t.x<WIDTH-100 and t.y>100 and t.y<250 then
-- do whatever when the right square is touched
s2.x=t.x    -- right square x,y positions
s2.y=t.y
end
end
end

``````

• Posts: 381

That's why I proposed gesture API since the beginning of Codea (was Codify). There are times when you just don't want to get messy with touch detection.

• Posts: 1,976

Simple. You just need to update the variable in the tabe. It's assigning a clone, not a pointer. Remove the first if statement to check if the touch's state is BEGAN. This should work.

``````function Player:touched(touch)
--Move
self.touches[touch.id]=touch
for k,touchs in pairs(self.touches) do
if touchs.state==MOVING and touchs.x>0 and touchs.x<150 and
touchs.y>0 and touchs.y<150 then
self.speed=touchs.y/9
end
--Look x and y
if touchs.state==BEGAN then
self.dax=touchs.x
self.lyx=touchs.y
end
if touchs.state==MOVING and touchs.x>150 then
self.da=self.dalx-(touchs.x-self.dax)
self.angle=self.da/80
self.tly=self.lylx-(self.lyx-touchs.y)
self.tly=self.tly/100
if self.tly>1.5 then self.tly=1.5 end
if self.tly<-1.5 then self.tly=-1.5 end
end
if touchs.state==ENDED then
self.touch[touchs.id]=nil
self.speed=0
self.dax=0
self.dalx=self.da
self.lylx=self.tly
self.lyx=0
end
end
end
``````
• Posts: 105

@SkyTheCoder @dave1707 @Andrew_Stacey Thank you! Now it works as it schould!