Movement Button Problem

Hello Community, I have a problem with my movement buttons; I have two buttons (Button Left and Button Right), I’ve added a “touch” listener to both buttons, and the multitouch is enable. When the “touch” starts the character moves to the indicated direction, and when the “touch” is ended the character is stopped; but here’s the problem: sometimes the character continues moving to any direction and doesn’t stop when I release the button. [import]uid: 81091 topic_id: 21552 reply_id: 321552[/import]

Do you have an ended phase on your buttons? [import]uid: 52491 topic_id: 21552 reply_id: 85467[/import]

Also you would want to have this at the end of your function.

[code]
return true [import]uid: 84637 topic_id: 21552 reply_id: 85643[/import]

@peach Yes, when e.phase “ended” = stop

@danny I tried it, but nothing happens [import]uid: 81091 topic_id: 21552 reply_id: 85905[/import]

Hi :slight_smile:

Can you strip the part in question down to an example/sample and post up the code?

I’m sure its something simple :slight_smile: [import]uid: 84637 topic_id: 21552 reply_id: 85909[/import]

This code works? Do you have the code that doesn’t work? [import]uid: 10389 topic_id: 21552 reply_id: 85946[/import]

The code that doesn’t work is the same, just replace the “Runtime Listener touch-moveLeft” and “Runtime Listener touch-moveRight” to two button listeners (“ButtonLeft Listener touch-moveLeft” and “ButtonRight Listener touch-moveRight”) and erase the “e.x” and “e.y” conditions inside moveLeft and moveRight functions. [import]uid: 81091 topic_id: 21552 reply_id: 85950[/import]

I changed the “e.x” and “e.y” coordinates (to a area of 50X50 pixels) to simulate the area of the buttons but I have the same problem, the problem doesn’t exist just when I use the huge area. [import]uid: 81091 topic_id: 21552 reply_id: 85952[/import]

Can you post the full code with button source just to humour me? :slight_smile: [import]uid: 10389 topic_id: 21552 reply_id: 85957[/import]

Yes, here’s the code, but in this code I changed the button’s listeners to a Runtime listener, you can see here, that if you touch inside a specific part of the screen you can move the character to right or left, but when I use the buttons (not the Runtime listener) I’m getting problems.

[code]
–Multitouch is Activated

–Buttons are in the left corner and right corner from the screen (down)
local buttonLeft = display.newImage ( “button.png” )
buttonLeft:addEventListener ( “touch”, moveLeft )
local buttonRight = display.newImage ( “button2.png” )
buttonRight:addEventListener ( “touch”, moveRight )

–Checking if Movement to Right is true
function moveRight (e)
if ( gameplay == true ) then
if ( e.phase == “began” ) then
motionRight = true
elseif ( e.phase == “ended” ) then
motionRight = false
end
end
return true
end

–Checking if Movement to Left is true
function moveLeft (e)
if ( gameplay == true ) then
if ( e.phase == “began” ) then
motionLeft = true
elseif ( e.phase == “ended” ) then
motionLeft = false
end
end
return true
end

–Movement Setup ( Fixing the double touch )
function movement ()
if ( gameplay == true ) then
if ( motionRight == true and motionLeft == false ) then
motionX = speedX
elseif ( motionRight == false and motionLeft == true ) then
motionX = -speedX
elseif ( motionRight == true and motionLeft == true ) then
motionX = 0
elseif ( motionRight == false and motionLeft == false ) then
motionX = 0
end
end
end

Runtime:addEventListener ( “enterFrame”, movement )

–Group Movement ( Player )
function groupMove ()
if ( gameplay == true ) then
player.x = player.x + motionX
end
end

Runtime:addEventListener ( “enterFrame”, groupMove )

–Player Wrap
function playerWrap ()
if ( gameplay == true ) then
if ( player.x < 150 ) then
player.x = 150
elseif ( player.x > wScreen - 25 ) then
player.x = wScreen - 25
end
end
end

Runtime:addEventListener ( “enterFrame”, playerWrap )
[/code] [import]uid: 81091 topic_id: 21552 reply_id: 85917[/import]

Ready, the code above is the code that doesn’t work. The problem is that my character sometimes doesn’t stop when I release the button and continues its movement. [import]uid: 81091 topic_id: 21552 reply_id: 85959[/import]

I wonder if you need to test for “cancelled” as well… [import]uid: 10389 topic_id: 21552 reply_id: 85963[/import]

eg:

[lua]elseif ( e.phase == “ended” or e.phase == “cancelled”) then[/lua] [import]uid: 10389 topic_id: 21552 reply_id: 85964[/import]

I think this thread has the answer your looking for :slight_smile:

http://developer.anscamobile.com/forum/2012/01/24/what-do-when-touch-target-lost [import]uid: 84637 topic_id: 21552 reply_id: 86035[/import]

@Danny Yeah, I think that’s the problem, but I could not fix it. The parameters to “ended” the touch when I release it outside the buttons don’t work. Sorry Danny, can you help me with that? I tried this:

--Checking if Movement to Right is true function moveRight (e) if ( gameplay == true ) then if ( e.phase == "began" ) then motionRight = true elseif ( e.phase == "ended" ) then motionRight = false if ( e.x \> buttonRight.contentBounds.xMin and e.x \< buttonRight.contentBounds.xMax and e.y \> buttonRight.contentBounds.yMin and e.y \< buttonRight.contentBounds.yMax ) then motionRight = false end end end print ( motionRight ) end [import]uid: 81091 topic_id: 21552 reply_id: 86947[/import]