Problem with Touch Event

Hello,
First of all this is my second post in Corona Forums, my name is Luis Jose Regis (just tell me joe), i'm from Peru and ive been using Corona SDK for around 3 Weeks, i think Corona is a great tool for mobile development and im surely going to suscribe to launch my first game when it's done.
I'm new to Corona SDK and also to Lua, i've been programming without problems so far, but i've found a little problem right now with the single touch event in my app.

The following code is supposed to be a character that moves to the right when a touch event is detected on the right of the character, otherwise if the touch event is detected to the left side of the character, he should move to the left.

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
function obj:touch(event)
print("touch event")
print("event("..event.phase)
 
 
if(event.phase == "ended")
then
                self.touched = self.touched-1
                print("Se acabo el movimiento")
                if(self.touched == 0)
                then
                print("Touchs "..self.touched)
                self.speedX = 0
                end
end
 
if(event.phase == "began") 
then
 
if(self.touched == 0 )
then
                if(event.x>self.x)
                then
                self.touched = self.touched+1
                self.speedX = 300
                
                end
                if(event.x<=self.x)
                then
                self.touched = self.touched+1
                self.speedX = -300
                end
                                print("Touchs "..self.touched)
end
 
end
Runtime:addEventListener("touch", obj)

are you looking for something like this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local ball = display.newCircle(150,100,40)
local speedX    
function balltouched(event)
        if(event.phase == "began") then
                  if(event.x>event.target.x) then
               speedX = 100
            end
            if(event.x<=event.target.x) then
               speedX = -100
            end
        elseif(event.phase == "ended") then
        transition.to(ball,{time =1000,x = ball.x + speedX})   
    end
end
        
ball:addEventListener("touch",balltouched)

Hi - I'm having what I think is a similar problem. I have touch listener on the background and move a character left or right with an enterFrame function depending on which side of the screen I touch. It works fine if you touch and release with one finger but not if you do the following:

Touch the right of the screen and the character moves right. Keep that finger on the screen to keep the character moving right. Now add a second finger to the screen while your original finger is still on the screen and lifting both fingers off the screen does not stop the character from moving.

Checking things with the following code shows that adding a second finger to the screen seems to prevent the touch ended phase from happening when both fingers are released from the screen, so the moveCharacter enterFrame listener is not removed. The next time you touch the screen another enterFrame listener is added. I've been able to remove all the listeners with a for do loop which happens onTouch eneded for a single finger touch and lift, but that doesn't help with the problem.

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
local centreX= display.contentWidth/2
 
local tX
 
local dirTxt = display.newText("direction",100 ,100, native.systemFont, 30)
 
local infoTxt = display.newText("phase",100 ,200, native.systemFont, 30)
 
local listTxt = display.newText("number of listeners 0",100 ,300, native.systemFont, 30)
 
local frameCount   --> a simple frame count to show when movement would be happening
                   --> set to 0 at onTouch began and incremented in moveCharacter
 
local listenerNum=0
 
local moveCharacter = function()
    frameCount=frameCount+1
    if tX>centreX then
      dirTxt.text="move right "..tostring(frameCount)
    elseif tX<centreX then
      dirTxt.text="move left "..tostring(frameCount)
    end
end
 
 
 
local onTouch = function(e)
    if e.phase=='began' then
      frameCount=0
      infoTxt.text='began'
      tX=e.x
      Runtime:addEventListener("enterFrame",moveCharacter)
      listenerNum=listenerNum+1
    elseif e.phase=='ended' then
      infoTxt.text='ended '
      local numListeners=listenerNum
      for c=1, numListeners do
          Runtime:removeEventListener("enterFrame",moveCharacter)
          listenerNum=listenerNum-1
      end
      dirTxt.text="no movement"
    end
end
 
 
 
local showNumListeners = function()
    listTxt.text="number of listeners "..tostring(listenerNum)
end
 
Runtime:addEventListener("enterFrame",showNumListeners)
Runtime:addEventListener("touch",onTouch)

Hi - I'm having what I think is a similar problem. I have touch listener on the background and move a character left or right with an enterFrame function depending on which side of the screen I touch. It works fine if you touch and release with one finger but not if you do the following:

Touch the right of the screen and the character moves right. Keep that finger on the screen to keep the character moving right. Now add a second finger to the screen while your original finger is still on the screen and lifting both fingers off the screen does not stop the character from moving.

Checking things with the following code shows that adding a second finger to the screen seems to prevent the touch ended phase from happening when both fingers are released from the screen, so the moveCharacter enterFrame listener is not removed. The next time you touch the screen another enterFrame listener is added. I've been able to remove all the listeners with a for do loop which happens onTouch eneded for a single finger touch and lift, but that doesn't help with the problem.

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
local centreX= display.contentWidth/2
 
local tX
 
local dirTxt = display.newText("direction",100 ,100, native.systemFont, 30)
 
local infoTxt = display.newText("phase",100 ,200, native.systemFont, 30)
 
local listTxt = display.newText("number of listeners 0",100 ,300, native.systemFont, 30)
 
local frameCount   --> a simple frame count to show when movement would be happening
                   --> set to 0 at onTouch began and incremented in moveCharacter
 
local listenerNum=0
 
local moveCharacter = function()
    frameCount=frameCount+1
    if tX>centreX then
      dirTxt.text="move right "..tostring(frameCount)
    elseif tX<centreX then
      dirTxt.text="move left "..tostring(frameCount)
    end
end
 
 
 
local onTouch = function(e)
    if e.phase=='began' then
      frameCount=0
      infoTxt.text='began'
      tX=e.x
      Runtime:addEventListener("enterFrame",moveCharacter)
      listenerNum=listenerNum+1
    elseif e.phase=='ended' then
      infoTxt.text='ended '
      local numListeners=listenerNum
      for c=1, numListeners do
          Runtime:removeEventListener("enterFrame",moveCharacter)
          listenerNum=listenerNum-1
      end
      dirTxt.text="no movement"
    end
end
 
 
 
local showNumListeners = function()
    listTxt.text="number of listeners "..tostring(listenerNum)
end
 
Runtime:addEventListener("enterFrame",showNumListeners)
Runtime:addEventListener("touch",onTouch)

Sorry for the double post - first time here and didn't realise 'save' posted! Oops...

views:1995 update:2011/10/11 15:24:38
corona forums © 2003-2011