Multi Touch

Hey guys,

So I have two images. I need the user to touch them both at the same time to activate a function. How do I do that?

 local image1 = display.newImageRect("image1.png", 100, 150)  
 image1:setReferencePoint(display.CenterReferencePoint)  
 image1.x = \_W/2  
 image1.y = 110  
 image1.rotation = 180  
  
local image2 = display.newImageRect("image2.png", 100, 150)  
 image2:setReferencePoint(display.CenterReferencePoint)  
 image2.x = \_W/2  
 image2.y = \_H - 100  
  

Any help would be great, Thanks! [import]uid: 95495 topic_id: 20437 reply_id: 320437[/import]

Hi!
First of all you will have to activate the multitouch function :

[lua]system.activate(“multitouch”)[/lua]

then i would just add one touch eventListener for each of one of the images.
I keep track of them by adding +1 to a local variable.

[lua]image1:addEventListener(“touch”,eventTouch)
image2:addEventListener(“touch”,eventTouch)

local counter = 0

function eventTouch(event)

if(event.phase == “began”) then

counter = counter + 1
end

if(event.phase == “ended” then

counter = counter - 1
end

if ( counter == 2) then

– Do whatever you like –

end
end[/lua] [import]uid: 103182 topic_id: 20437 reply_id: 80067[/import]

Hey, thanks!

Should I activate multitouch on my main.lua? [import]uid: 95495 topic_id: 20437 reply_id: 80069[/import]

Yes, you should probably activate multi-touch in main.lua
[import]uid: 19626 topic_id: 20437 reply_id: 80088[/import]

Ok, my multi touch is working.

However, my function with counter == 2 isn’t working. I printed the counter, and it is adding correctly by touching. Here’s what I need it to do when both images are pressed:[code]

local function onTimer(event)
if flag then
print(“The button was pressed for 4 seconds”)
local event = {}
event.phase = “ended”
image1:dispatchEvent( event )
director:changeScene(“scene2”)
else
end
end

local function onTouch(event)
if (counter == 2) then
flag = true
timerHandle = timer.performWithDelay(4000,onTimer)
else
flag = false
timer.cancel(timerHandle)
timerHandle = nil
end
end[/code]

So, explaining: only once the user starts pressing both images, a timer starts. And only if he keeps holding it for 4 seconds, the scene will change. Otherwise, it will cancel the timer.

Any thoughts? [import]uid: 95495 topic_id: 20437 reply_id: 80481[/import]

Guys? Can you give me a hand? [import]uid: 95495 topic_id: 20437 reply_id: 80810[/import]