event.phase not ends after stop press

Hi .

I had been experiencing a bug before but I ended up giving up trying to fix it after every day checking.

Only he came to haunt me again, and this time I definitely need to sort this out because I need this function …

So the problem is this:

I needed to press a button and this button presents me with an image on the screen, but after removing my finger from the touch, not always the function understands that I stopped pressing, and sometimes it simply goes into a loop and continues to eternally present the image on the screen as if I was pressing.

I checked the function and found that sometimes it does not pass through the stage (event.phase == “ended” or event.phase == “canceled”).

Can anyone explain me what I did wrong in this function?

 local function Zerar( event ) TapOuTouch = 0.01 end function TouchBotoes(event) Runtime:removeEventListener("enterFrame", Zerar) NomePressionado = event.target.name if (event.phase == "began" ) then function TempoPressionado( event ) TapOuTouch = TapOuTouch + 0.01 if ModeloNaTela == false then if (TapOuTouch \> 0.4) then if AmostraCaminhao == nil then ModeloNaTela = true -- img end end end end Runtime:addEventListener("enterFrame", TempoPressionado) end if ( event.phase == "moved" ) then -- nothing end if ( event.phase == "ended" or event.phase == "cancelled" ) then Runtime:removeEventListener("enterFrame", TempoPressionado) if (TapOuTouch \< 0.4) then -- code botaoPressionado() else -- remove img end Runtime:addEventListener("enterFrame", Zerar) end return true end

There are a couple of suggestions I would make. First, I’m not fond of nested functions, in particular when you have to make them global for visibility elsewhere. I don’t see any reason why this function:

 function TempoPressionado( event ) TapOuTouch = TapOuTouch + 0.01 if ModeloNaTela == false then if (TapOuTouch \> 0.4) then if AmostraCaminhao == nil then ModeloNaTela = true -- img end end end end

Can’t be moved outside of your touch handler and made local. 

Next, you really should be putting a print statement at the top of your touch handler that prints out the event table or at a minimum event.phase so you can see what event’s you’re getting.

You also don’t show us your image creation code. Are you adding a touch listener to your popup image? Could it be stealing the touch?

Rob

That listener has a some issues.

  1. It isn’t really designed for use with a specific object.

  2. It isn’t taking focus so it won’t receive all events.

This is better:

local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end local function touch( self, event ) local phase = event.phase local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) elseif( self.isFocus ) then if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end

Later you’d use the listener like this:

local obj = display.new...( ... ) -- make an object; how doesn't matter obj.touch = touch obj:addEventListener("touch") 

This object will received all events associated with its touch till the object is destroyed or the touch ends, which ever comes first.

Also note.  I would not use an enterFrame listener as some kind of way to make the button look like it is pressed.  That will result in all kinds of terrible problems for you.

Simply change the visual attributes you want in began and reset them to the default values in ended.

Additionally,  you can use my ‘inBounds’ code to switch back and forth when the finger is moved off and then back on to the image.

In this example the code has been modified to highlight the display object as ‘red’ when pressed.

local function touch( self, event ) local phase = event.phase local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) obj:setFillColor(1,0,0) elseif( self.isFocus ) then if( inBounds ) then obj:setFillColor(1,0,0) else obj:setFillColor(1,1,1) end if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) obj:setFillColor(1,1,1) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end

Final note.  I just saw Rob’s post and he is right on everything he said, and I too worry you may have another listener somewhere else that is stealing the focus.  If it does, my listener won’t help because you are overriding focus.

It’s relatively impossible for me to apply all these changes.

Why?

Because he did not bother working with just one image. My check if the button was pressed is a vector with a maximum of 35 objects.

Each object has its check, and each object calls a different image. (I did not actually post the whole code because of the confusion you were going to make upon seeing it)

For example @roaming, I started messing up your code, however you deduced that it was just an object, so in your first function you use everything as “obj.xe obj.y” but I have no idea what the object is, you see? I can not put random values ​​…

I need to differentiate the TAP from the TOUCH, and if it is just a TAP it performs a function that is just add 1 to the counter and if it is TOUCH it shows the image on the screen, if the TOUCH stops being presonized then the image on the screen disappears.

“tidying up” your code would have something like:

-- That is impossible, not exsit only one object, the object of tap/touch, is the botoes{} (a random value, min 1 max 35) local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end local function TouchBotoes( self, event ) local phase = event.phase NomePressionado = event.target.name local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) AmostraCaminhao = display.newImageRect("imagens/Caminhao".. NomePressionado .. ".png", bottomMarg/2, rightMarg/2) AmostraCaminhao.x = display.contentCenterX AmostraCaminhao.y = display.contentCenterY AmostraCaminhao:rotate( 90 ) AmostraCaminhao.alpha = 0.5 transition.to( AmostraCaminhao, { time=350, alpha=1.0 } ) transition.to( bg, { time=250, alpha=0.5 } ) for i=1,#Categorias do transition.to( botoes[i], { time=250, alpha=0.5 } ) end elseif( self.isFocus ) then if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end for i=1,QuantidadeCategorias do if i==1 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) elseif i \> 1 and i \< 8 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 8 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 8 and i \< 15 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 15 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 15 and i \< 22 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 22 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 22 and i \< 29 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 29 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 29 and i \< 36 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end -- Parametro de correção if QuantidadeRepetidas \> 2 then QuantidadeRepetidas = 2 end botoes[i].name = Categorias[i] botoes[i]:addEventListener("touch", TouchBotoes) group:insert( botoes[i] ) end

In response to @ROB, there was this confusion of functions precisely so as not to have thousands of functions scattered throughout the code.

Here is the original code for you to understand how boring this is:

PS: " NomePressionado = event.target.name " refers to the botoes[i].name

 function TouchBotoes(event) TouchCancelX = event.x TouchCancelY = event.y Runtime:removeEventListener("enterFrame", Zerar) NomePressionado = event.target.name if (event.phase == "began" ) then function TempoPressionado( event ) TapOuTouch = TapOuTouch + 0.01 --print(TapOuTouch) if ModeloNaTela == false then if (TapOuTouch \> 0.4) then if AmostraCaminhao == nil then ModeloNaTela = true AmostraCaminhao = display.newImageRect("imagens/Caminhao".. NomePressionado .. ".png", bottomMarg/2, rightMarg/2) AmostraCaminhao.x = display.contentCenterX AmostraCaminhao.y = display.contentCenterY AmostraCaminhao:rotate( 90 ) AmostraCaminhao.alpha = 0.5 transition.to( AmostraCaminhao, { time=350, alpha=1.0 } ) transition.to( bg, { time=250, alpha=0.5 } ) for i=1,#Categorias do transition.to( botoes[i], { time=250, alpha=0.5 } ) end end end end end Runtime:addEventListener("enterFrame", TempoPressionado) end if ( event.phase == "moved" ) then if event.x \> TouchCancelX + 10 or event.y \> TouchCancelY + 10 then Runtime:addEventListener("enterFrame", Zerar) Runtime:removeEventListener("enterFrame", TempoPressionado) if (TapOuTouch \> 0.4) then local function listener1( obj ) display.remove(AmostraCaminhao) AmostraCaminhao = nil ModeloNaTela = false TapOuTouch = 0.01 end transition.to( AmostraCaminhao, { time=350, alpha=0, onComplete = listener1 } ) transition.to( bg, { time=350, alpha=1.0 } ) for i=1,#Categorias do transition.to( botoes[i], { time=350, alpha=1.0 } ) end end end end if ( event.phase == "ended" or event.phase == "cancelled" ) then Runtime:removeEventListener("enterFrame", TempoPressionado) if (TapOuTouch \< 0.4) then local NomeBotao = event.target function ReAparecer(event) transition.to( NomeBotao, { time=100, alpha=1.0 } ) end transition.to(NomeBotao, { time=100, alpha=0, onComplete=ReAparecer } ) print("tap") print(event.target.name) botaoPressionado() else local function listener3( event ) display.remove(AmostraCaminhao) AmostraCaminhao = nil ModeloNaTela = false TapOuTouch = 0.01 print("excluido") end transition.to( AmostraCaminhao, { time=350, alpha=0, onComplete = listener3 } ) transition.to( bg, { time=350, alpha=1.0 } ) for i=1,#Categorias do transition.to( botoes[i], { time=350, alpha=1.0 } ) end end Runtime:addEventListener("enterFrame", Zerar) end return true end --- Disponibilizar as imagens dos botões na tela for i=1,QuantidadeCategorias do if i==1 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) elseif i \> 1 and i \< 8 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 8 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 8 and i \< 15 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 15 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 15 and i \< 22 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 22 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 22 and i \< 29 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 29 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 29 and i \< 36 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end -- Parametro de correção if QuantidadeRepetidas \> 2 then QuantidadeRepetidas = 2 end botoes[i].name = Categorias[i] botoes[i]:addEventListener("touch", TouchBotoes) group:insert( botoes[i] ) end

Honestly I’m VERY lost from what I do here, it’s because this case is very specific. of not knowing which button the user is pressing, and the button that the user is pressing has the same name as the image that I will upload, so I need everything in order because I do not know the name of the image that will appear on the screen and nor the name of the button that the user is prescribing.

There are a couple of suggestions I would make. First, I’m not fond of nested functions, in particular when you have to make them global for visibility elsewhere. I don’t see any reason why this function:

 function TempoPressionado( event ) TapOuTouch = TapOuTouch + 0.01 if ModeloNaTela == false then if (TapOuTouch \> 0.4) then if AmostraCaminhao == nil then ModeloNaTela = true -- img end end end end

Can’t be moved outside of your touch handler and made local. 

Next, you really should be putting a print statement at the top of your touch handler that prints out the event table or at a minimum event.phase so you can see what event’s you’re getting.

You also don’t show us your image creation code. Are you adding a touch listener to your popup image? Could it be stealing the touch?

Rob

That listener has a some issues.

  1. It isn’t really designed for use with a specific object.

  2. It isn’t taking focus so it won’t receive all events.

This is better:

local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end local function touch( self, event ) local phase = event.phase local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) elseif( self.isFocus ) then if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end

Later you’d use the listener like this:

local obj = display.new...( ... ) -- make an object; how doesn't matter obj.touch = touch obj:addEventListener("touch") 

This object will received all events associated with its touch till the object is destroyed or the touch ends, which ever comes first.

Also note.  I would not use an enterFrame listener as some kind of way to make the button look like it is pressed.  That will result in all kinds of terrible problems for you.

Simply change the visual attributes you want in began and reset them to the default values in ended.

Additionally,  you can use my ‘inBounds’ code to switch back and forth when the finger is moved off and then back on to the image.

In this example the code has been modified to highlight the display object as ‘red’ when pressed.

local function touch( self, event ) local phase = event.phase local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) obj:setFillColor(1,0,0) elseif( self.isFocus ) then if( inBounds ) then obj:setFillColor(1,0,0) else obj:setFillColor(1,1,1) end if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) obj:setFillColor(1,1,1) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end

Final note.  I just saw Rob’s post and he is right on everything he said, and I too worry you may have another listener somewhere else that is stealing the focus.  If it does, my listener won’t help because you are overriding focus.

It’s relatively impossible for me to apply all these changes.

Why?

Because he did not bother working with just one image. My check if the button was pressed is a vector with a maximum of 35 objects.

Each object has its check, and each object calls a different image. (I did not actually post the whole code because of the confusion you were going to make upon seeing it)

For example @roaming, I started messing up your code, however you deduced that it was just an object, so in your first function you use everything as “obj.xe obj.y” but I have no idea what the object is, you see? I can not put random values ​​…

I need to differentiate the TAP from the TOUCH, and if it is just a TAP it performs a function that is just add 1 to the counter and if it is TOUCH it shows the image on the screen, if the TOUCH stops being presonized then the image on the screen disappears.

“tidying up” your code would have something like:

-- That is impossible, not exsit only one object, the object of tap/touch, is the botoes{} (a random value, min 1 max 35) local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end local function TouchBotoes( self, event ) local phase = event.phase NomePressionado = event.target.name local id = event.id local inBounds = isInBounds( event, self ) if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) AmostraCaminhao = display.newImageRect("imagens/Caminhao".. NomePressionado .. ".png", bottomMarg/2, rightMarg/2) AmostraCaminhao.x = display.contentCenterX AmostraCaminhao.y = display.contentCenterY AmostraCaminhao:rotate( 90 ) AmostraCaminhao.alpha = 0.5 transition.to( AmostraCaminhao, { time=350, alpha=1.0 } ) transition.to( bg, { time=250, alpha=0.5 } ) for i=1,#Categorias do transition.to( botoes[i], { time=250, alpha=0.5 } ) end elseif( self.isFocus ) then if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) -- The touch effectively ended do whatever you want for ended here. if( inBounds ) then -- You may want to restrict actions to only work if the finger -- is ON the button when lifted. -- Do that here... end else -- This is a move do whatever you want for moves here. end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end return true end for i=1,QuantidadeCategorias do if i==1 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) elseif i \> 1 and i \< 8 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 8 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 8 and i \< 15 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 15 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 15 and i \< 22 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 22 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 22 and i \< 29 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 29 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 29 and i \< 36 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end -- Parametro de correção if QuantidadeRepetidas \> 2 then QuantidadeRepetidas = 2 end botoes[i].name = Categorias[i] botoes[i]:addEventListener("touch", TouchBotoes) group:insert( botoes[i] ) end

In response to @ROB, there was this confusion of functions precisely so as not to have thousands of functions scattered throughout the code.

Here is the original code for you to understand how boring this is:

PS: " NomePressionado = event.target.name " refers to the botoes[i].name

 function TouchBotoes(event) TouchCancelX = event.x TouchCancelY = event.y Runtime:removeEventListener("enterFrame", Zerar) NomePressionado = event.target.name if (event.phase == "began" ) then function TempoPressionado( event ) TapOuTouch = TapOuTouch + 0.01 --print(TapOuTouch) if ModeloNaTela == false then if (TapOuTouch \> 0.4) then if AmostraCaminhao == nil then ModeloNaTela = true AmostraCaminhao = display.newImageRect("imagens/Caminhao".. NomePressionado .. ".png", bottomMarg/2, rightMarg/2) AmostraCaminhao.x = display.contentCenterX AmostraCaminhao.y = display.contentCenterY AmostraCaminhao:rotate( 90 ) AmostraCaminhao.alpha = 0.5 transition.to( AmostraCaminhao, { time=350, alpha=1.0 } ) transition.to( bg, { time=250, alpha=0.5 } ) for i=1,#Categorias do transition.to( botoes[i], { time=250, alpha=0.5 } ) end end end end end Runtime:addEventListener("enterFrame", TempoPressionado) end if ( event.phase == "moved" ) then if event.x \> TouchCancelX + 10 or event.y \> TouchCancelY + 10 then Runtime:addEventListener("enterFrame", Zerar) Runtime:removeEventListener("enterFrame", TempoPressionado) if (TapOuTouch \> 0.4) then local function listener1( obj ) display.remove(AmostraCaminhao) AmostraCaminhao = nil ModeloNaTela = false TapOuTouch = 0.01 end transition.to( AmostraCaminhao, { time=350, alpha=0, onComplete = listener1 } ) transition.to( bg, { time=350, alpha=1.0 } ) for i=1,#Categorias do transition.to( botoes[i], { time=350, alpha=1.0 } ) end end end end if ( event.phase == "ended" or event.phase == "cancelled" ) then Runtime:removeEventListener("enterFrame", TempoPressionado) if (TapOuTouch \< 0.4) then local NomeBotao = event.target function ReAparecer(event) transition.to( NomeBotao, { time=100, alpha=1.0 } ) end transition.to(NomeBotao, { time=100, alpha=0, onComplete=ReAparecer } ) print("tap") print(event.target.name) botaoPressionado() else local function listener3( event ) display.remove(AmostraCaminhao) AmostraCaminhao = nil ModeloNaTela = false TapOuTouch = 0.01 print("excluido") end transition.to( AmostraCaminhao, { time=350, alpha=0, onComplete = listener3 } ) transition.to( bg, { time=350, alpha=1.0 } ) for i=1,#Categorias do transition.to( botoes[i], { time=350, alpha=1.0 } ) end end Runtime:addEventListener("enterFrame", Zerar) end return true end --- Disponibilizar as imagens dos botões na tela for i=1,QuantidadeCategorias do if i==1 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) elseif i \> 1 and i \< 8 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 8 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 8 and i \< 15 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 15 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 15 and i \< 22 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 22 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 22 and i \< 29 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end if i == 29 then QuantidadeRepetidas = 1 botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = display.contentCenterX/0.61 - 35 - 35 - 35 - 35 botoes[i].y = display.contentCenterY botoes[i]:rotate( 90 ) end if i \> 29 and i \< 36 then if (i%2) == 0 then -- Quando o numero é par botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) print(i .. " " .. QuantidadeRepetidas) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y -75 botoes[i]:rotate( 90 ) else -- Quando o número é impar botoes[i] = display.newImageRect("imagens/".. Categorias[i] .. ".png", 70, 30) botoes[i].x = botoes[i-QuantidadeRepetidas].x botoes[i].y = botoes[i-QuantidadeRepetidas].y + 75 botoes[i]:rotate( 90 ) end QuantidadeRepetidas = QuantidadeRepetidas + 1 end -- Parametro de correção if QuantidadeRepetidas \> 2 then QuantidadeRepetidas = 2 end botoes[i].name = Categorias[i] botoes[i]:addEventListener("touch", TouchBotoes) group:insert( botoes[i] ) end

Honestly I’m VERY lost from what I do here, it’s because this case is very specific. of not knowing which button the user is pressing, and the button that the user is pressing has the same name as the image that I will upload, so I need everything in order because I do not know the name of the image that will appear on the screen and nor the name of the button that the user is prescribing.