Detecting Two Touches to Trigger an Event

Hi guys,

I’m trying to trigger an event when the player touches two virtual buttons at the same time but can’t seem to find any tutorials on it. Hope you guys can help me out.  :slight_smile:

Basically my game has two virtual buttons; L and R

  • pressing L will make the character move left.
  • pressing R will make the character move right.
  • pressing L and R together will make the character jump <-- this is where I’m having problems. I don’t know how to check if both buttons were pressed.

Thanks in advance.

Below is my code

-- L Button Declarations etc LButton.myName = "leftSquare" LButton:addEventListener("touch", touchControls) -- R Button Declarations etc RButton.myName = "rightSquare" RButton:addEventListener("touch", touchControls) function touchControls(event) if (event.phase == "began") then if (event.target.myName == "leftSquare") then -- Character Moving Left elseif (event.target.myName == "rightSquare") then -- Character Moving Right end end if (event.phase == "ended") then -- Stop Character Movement end return true end

You will need to store a value for each button which indicates their states (pressed/released.) When a touch event is fired you can then check whether both buttons are pressed at the same time and perform a jump.

Let me check my code - I have something like that in a project.

Hmmm… Here’s the relevant part of my code - mind you, it won’t work out of the box for you, you’ll have to use this as a starting point and roll your own. FYI, I don’t use buttons, I just detect if a touch was on the left part or the right part of the screen.

if event.phase == "began" then if touchControl.touchID1 == false then -- touch1 not yet touching until now touchControl.touchID1 = event.id if event.x \< 1024 then touching = "left" elseif event.x \> 1023 then touching = "right" end else -- touch1 was already touching jumpTouch = true end elseif event.phase == "ended" or event.phase == "cancelled" then if event.id == touchControl.touchID1 then touchControl.touchID1 = false touching = false end end

I would have thought that creating a couple of buttons each with a name/id/tag which use the same listener function that also performs an action based on the number of buttons currently pressed would do the job. Something like:

local buttonA = display.newCircle( 100, 100, 25 ) local buttonB = display.newCircle( 200, 100, 25 ) local pressData = { count=0, ifLeftPressed=false, ifRightPressed=false } local function performPressAction() if (pressData.count == 0) then print("called when all buttons are released") elseif (pressData.count == 1) then print("called when only one button is pressed") if (pressData.ifLeftPressed) then print("Left is pressed") else print("Right is pressed") end elseif (pressData.count == 2) then print("called when all buttons are pressed") else print("unknown situation") end end local function touch(e) if (e.phase == "began") then pressData.count = pressData.count + 1 e.target.hasFocus = true display.currentStage:setFocus( e.target, e.id ) elseif (e.target.hasFocus) then if (e.phase == "moved") then else pressData.count = pressData.count - 1 e.target.hasFocus = false display.currentStage:setFocus( e.target, nil ) end end performPressAction() return true end buttonA:addEventListener("touch",touch) buttonB:addEventListener("touch",touch)

@horacebury @thomas6

Thank you both for your inputs :). Will give both of them a try and see which one works best. 

After I’ve posted my question here, I did somehow manage to get it to work, but doesn’t seem to be optimized. 

You will need to store a value for each button which indicates their states (pressed/released.) When a touch event is fired you can then check whether both buttons are pressed at the same time and perform a jump.

Let me check my code - I have something like that in a project.

Hmmm… Here’s the relevant part of my code - mind you, it won’t work out of the box for you, you’ll have to use this as a starting point and roll your own. FYI, I don’t use buttons, I just detect if a touch was on the left part or the right part of the screen.

if event.phase == "began" then if touchControl.touchID1 == false then -- touch1 not yet touching until now touchControl.touchID1 = event.id if event.x \< 1024 then touching = "left" elseif event.x \> 1023 then touching = "right" end else -- touch1 was already touching jumpTouch = true end elseif event.phase == "ended" or event.phase == "cancelled" then if event.id == touchControl.touchID1 then touchControl.touchID1 = false touching = false end end

I would have thought that creating a couple of buttons each with a name/id/tag which use the same listener function that also performs an action based on the number of buttons currently pressed would do the job. Something like:

local buttonA = display.newCircle( 100, 100, 25 ) local buttonB = display.newCircle( 200, 100, 25 ) local pressData = { count=0, ifLeftPressed=false, ifRightPressed=false } local function performPressAction() if (pressData.count == 0) then print("called when all buttons are released") elseif (pressData.count == 1) then print("called when only one button is pressed") if (pressData.ifLeftPressed) then print("Left is pressed") else print("Right is pressed") end elseif (pressData.count == 2) then print("called when all buttons are pressed") else print("unknown situation") end end local function touch(e) if (e.phase == "began") then pressData.count = pressData.count + 1 e.target.hasFocus = true display.currentStage:setFocus( e.target, e.id ) elseif (e.target.hasFocus) then if (e.phase == "moved") then else pressData.count = pressData.count - 1 e.target.hasFocus = false display.currentStage:setFocus( e.target, nil ) end end performPressAction() return true end buttonA:addEventListener("touch",touch) buttonB:addEventListener("touch",touch)

@horacebury @thomas6

Thank you both for your inputs :). Will give both of them a try and see which one works best. 

After I’ve posted my question here, I did somehow manage to get it to work, but doesn’t seem to be optimized.