Movement Button Problem

14 replies [Last post]
devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

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.

Replies

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

Do you have an ended phase on your buttons?

Danny's picture
Danny
User offline. Last seen 1 hour 23 min ago. Offline
Staff
Joined: 17 Aug 2011

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

1
return true

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

@peach Yes, when e.phase "ended" = stop

@danny I tried it, but nothing happens

Danny's picture
Danny
User offline. Last seen 1 hour 23 min ago. Offline
Staff
Joined: 17 Aug 2011

Hi :)

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

I'm sure its something simple :)

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--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 )

WauloK's picture
WauloK
User offline. Last seen 3 days 7 hours ago. Offline
Joined: 21 Oct 2010

This code works? Do you have the code that doesn't work?

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

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.

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

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.

WauloK's picture
WauloK
User offline. Last seen 3 days 7 hours ago. Offline
Joined: 21 Oct 2010

Can you post the full code with button source just to humour me? :)

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

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.

WauloK's picture
WauloK
User offline. Last seen 3 days 7 hours ago. Offline
Joined: 21 Oct 2010

I wonder if you need to test for "cancelled" as well...

WauloK's picture
WauloK
User offline. Last seen 3 days 7 hours ago. Offline
Joined: 21 Oct 2010

eg:

1
elseif ( e.phase == "ended" or e.phase == "cancelled") then

Danny's picture
Danny
User offline. Last seen 1 hour 23 min ago. Offline
Staff
Joined: 17 Aug 2011

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

http://developer.anscamobile.com/forum/2012/01/24/what-do-when-touch-target-lost

devsalad's picture
devsalad
User offline. Last seen 6 weeks 19 hours ago. Offline
Joined: 4 Aug 2011

@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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--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

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.