Moving a ball on screen

I made two invisible rectangles on the screen. one on the left side and one on the right side. I have a ball in the middle and I want it to move left or right when I touch them. It works. The problem: I want that if I press one of them and after that I press the other (While the first still pressed) it will move the othe side but that not happen. for example: I press the right rectangle (The ball moves right) and while it pressed I press the left rectangle, but the ball still goes to the right.

code:

function moveLeft( e ) &nbsp; &nbsp; if (circle.x\>\_W\*0.031 ) then &nbsp; &nbsp; if (e.phase=="began") then &nbsp; &nbsp; circle:setLinearVelocity( -800\*v\_circle, 0 ) &nbsp; &nbsp; end &nbsp; &nbsp; if (e.phase=="ended") then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; if (e.x\>\_W\*0.44) then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end end end function moveRight( e ) &nbsp; &nbsp; if (circle.x\<\_W\*0.969) then &nbsp; &nbsp; if (e.phase=="began") then &nbsp; &nbsp; circle:setLinearVelocity( 800\*v\_circle, 0 ) &nbsp; &nbsp; end &nbsp; &nbsp; if (e.phase=="ended") then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; if (e.x\<\_W\*0.556) then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end &nbsp; &nbsp; end end &nbsp; &nbsp; clickLeft = display.newRect( \_W\*0.212, \_H/2, \_W\*0.7, \_H\*1.2 ) &nbsp; &nbsp; clickRight = display.newRect( \_W\*0.78, \_H/2, \_W\*0.7, \_H\*1.2 ) &nbsp; &nbsp; clickLeft.isVisible = false &nbsp; &nbsp; clickRight.isVisible = false &nbsp; &nbsp; clickLeft:addEventListener( "touch", moveLeft ) &nbsp; &nbsp; clickRight:addEventListener( "touch", moveRight )

I have found more - I have put this code in moveLeft function: (Between ****)

function moveLeft( e ) &nbsp; &nbsp; if (circle.x\>\_W\*0.031 ) then &nbsp; &nbsp; if (e.phase=="began") then &nbsp; &nbsp; circle:setLinearVelocity( -800\*v\_circle, 0 ) &nbsp; &nbsp; \*\*\*\* txt = display.newText("@@@@@@@", \_W/2, \_H\*0.57, "Wekar" , 115 ) \*\*\*\* &nbsp; &nbsp; end &nbsp; &nbsp; if (e.phase=="ended") then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; if (e.x\>\_W\*0.44) then &nbsp; &nbsp; circle:setLinearVelocity( 0, 0 ) &nbsp; &nbsp; end end end

And if I press the right rectangle and after that I press the left (While the first still pressed) it doesn’t show nothing.

Namely, in this situation it doesn’t even get into the moveLefr function.

please someone can help me? 

You could use two flags like this:

local leftDown = false local rightDown = false

And use them to keep track of each button.

In your moveLeft function, set leftDown to true in the begin phase and to false in the ended phase.

Do the same with rightDown and your moveRight function.

Then in your moveLeft function for example, you only need to set the linear velocity if rightDown is false:

if (e.phase=="began") and (rightDown == false) then -- set velocity -- set leftDown to true end

I want the opposite. I want it will move to the side that last touched.

Have you turned on multitouch?

system.activate( "multitouch" )

No. Can you explain?

You can read here: https://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

Basically, by default corona only deals with one touch at a time. To handle multiple simultaneous touches, you need to tell corona to do so, which is easily done, you just put the line from my last post into your code.

And it can make the app slower? If not why isn’t it default?
Do you understands what I mean?

I guess it would make the app slower. But then having eight physics bodies instead of seven will make the app slower, but that doesn’t mean you can’t comfortably have many more physics bodies. Likewise, turning on multitouch isn’t going to have much of an impact on performance. For what you are trying to do, I don’t believe there is an alternative.

As for why multitouch isn’t default, I don’t know. I’m sure they had their reasons. Maybe just for the sake of that tiny performance boost.

So it is insignificant?

Most people don’t want multi-touch on by default. 

Rob

Pretty much. Lot’s of apps use multitouch and run just fine.

Have you tried it yet? Do, and you’ll see it works fine.

Thanks.

You could use two flags like this:

local leftDown = false local rightDown = false

And use them to keep track of each button.

In your moveLeft function, set leftDown to true in the begin phase and to false in the ended phase.

Do the same with rightDown and your moveRight function.

Then in your moveLeft function for example, you only need to set the linear velocity if rightDown is false:

if (e.phase=="began") and (rightDown == false) then -- set velocity -- set leftDown to true end

I want the opposite. I want it will move to the side that last touched.

Have you turned on multitouch?

system.activate( "multitouch" )

No. Can you explain?

You can read here: https://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

Basically, by default corona only deals with one touch at a time. To handle multiple simultaneous touches, you need to tell corona to do so, which is easily done, you just put the line from my last post into your code.

And it can make the app slower? If not why isn’t it default?
Do you understands what I mean?

I guess it would make the app slower. But then having eight physics bodies instead of seven will make the app slower, but that doesn’t mean you can’t comfortably have many more physics bodies. Likewise, turning on multitouch isn’t going to have much of an impact on performance. For what you are trying to do, I don’t believe there is an alternative.

As for why multitouch isn’t default, I don’t know. I’m sure they had their reasons. Maybe just for the sake of that tiny performance boost.

So it is insignificant?