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.

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)  

Note: The var speedX is later used to make a move() method self.x = self.x + self.speedX

It works really well if you only use one finger to make the movement, but when it detects another touch event before the first one has been released it looks like it calls the “ended” event of the first touch, leaving your character without any move.

What i need is a way to prevent any other touch event after the first one has “ended”, tried to make it with a var “touched”, a counter of touchs, so the speed of the character will only be 0 when the “touched” var is 0, however it didn’t worked, you never have more than 1 touch and as i though it calls the “ended” of the first one when i touch the screen for second time.

Any Ideas? Thank you

Joe

[import]uid: 53575 topic_id: 11563 reply_id: 311563[/import]

are you looking for something like this ?
[lua]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)[/lua]

what do you mean by " when it detects another touch event before the first one has been released" are you moving the touch outside of the object before the touch is released ? in that case you need to take care of where the focus of touch is. you should do something like what given below
[lua]if event.phase == “began” then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
elseif event.target.isFocus then
if event.phase == “moved” then
–do something
elseif event.phase == “ended” or event.phase == "canceled"then
–do something
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end[/lua]
[import]uid: 71210 topic_id: 11563 reply_id: 49723[/import]

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.

[code]
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 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)
[/code]

Any ideas of how to sort it so that the movement stops when both fingers are lifted or, better still, so that the direction changes depending on which finger is lifted?

Thanks
Pete [import]uid: 24031 topic_id: 11563 reply_id: 52697[/import]

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.

[code]
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 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)
[/code]

Any ideas of how to sort it so that the movement stops when both fingers are lifted or, better still, so that the direction changes depending on which finger is lifted?

Thanks
Pete [import]uid: 24031 topic_id: 11563 reply_id: 52698[/import]

Sorry for the double post - first time here and didn’t realise ‘save’ posted! Oops… [import]uid: 24031 topic_id: 11563 reply_id: 52700[/import]