Conflicting touch events

Hey, I have two touch events in my coding for a came i’m making.

Here is the code:

function activateball(self, event)

  self:applyForce( 0.3, 0 )

end 

function moveright( event )

  --print(“touch”)

  if ( event.phase == “began” and event.x >

    display.contentWidth*.5 ) then

    ball.enterFrame = activateball

   Runtime:addEventListener(“enterFrame”, ball)

  end

  if event.phase == “ended” then 

  Runtime:removeEventListener(“enterFrame”, ball)

   

end

   

 end  

Runtime:addEventListener(“touch”, moveright)

function activateball(self, event)

  self:applyForce( -0.3, 0 )

end

function moveleft( event )

  

  if ( event.phase == “began” and event.x <

    display.contentWidth*.5 ) then

    ball.enterFrame = activateball

   Runtime:addEventListener(“enterFrame”, ball)

      

  end

  if event.phase == “ended” then 

  Runtime:removeEventListener(“enterFrame”, ball)

   

   

end

    

 end  

Runtime:addEventListener(“touch”, move left)

When I made the touch event “moveright” it worked fine, but when i added “moveleft” it made the object always move left no matter where I touch.  Sorry if this is a dumb question, it’s my first time ever building a game so I really don’t know very much about it at all.  Thanks.

Hi @PhonyRider,

This is a simple correction: you just need to “return true” at the end of your touch handling functions to prevent one touch from propagating through to objects below. See this guide for details and a complete explanation:

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Also, you are adding two Runtime touch listeners, which could be a problem, and it’s unnecessary. You only need one Runtime listener for this, and then you should combine these two functions into one and handle them conditionally, since they do very similar things.

Take care,

Brent

Thank you!

Hi @PhonyRider,

This is a simple correction: you just need to “return true” at the end of your touch handling functions to prevent one touch from propagating through to objects below. See this guide for details and a complete explanation:

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Also, you are adding two Runtime touch listeners, which could be a problem, and it’s unnecessary. You only need one Runtime listener for this, and then you should combine these two functions into one and handle them conditionally, since they do very similar things.

Take care,

Brent

Thank you!