I can not remove object

Hello, I have a rectangle inside a scrollview and when I touch the rectangle, another rectangle appears and I add a tap event on it, I would like to touch the rectangle in it, but my touch event works, but the object does not disappear.

local button function scene:create( event )     local sceneGroup = self.view scrollView = widget.newScrollView(     {       top = display.contentHeight - display.contentHeight \* 55 / 100,       left = 0,       width = display.contentWidth,       height = display.contentHeight \* 55 / 100,       scrollWidth = 600,       horizontalScrollDisabled = true,       scrollHeight = 1300,       listener = scrollListener     }   )       sceneGroup:insert(scrollView) for i=1, 86 do     cards[#cards + 1] = {          name = 'card'..i..'.png',          x    = 8 + ( i - 1 ) % 4 \* 76,         y    = 8 + ( mFloor( ( i - 0.5 ) / 4 ) \* 98 ),         w    = (display.contentWidth - 40) / 4,         h    = 90     }   end  function cardRemove(event)                               button:removeSelf()     button = nil     print("Toquei no Botão!")     print(button) -- Return nil.        end local function tap( self, event )     if lastTappedCard then         myCardsRef[lastTappedCard].alpha = 1          if button ~= nil then         button:removeSelf()         button = nil         end     end      myCardsRef[self].alpha = 0.5        button = display.newRect(sceneGroup,myCardsRef[self].x + myCardsRef[self].width \* .5,myCardsRef[self].y + myCardsRef[self].height \* .5, 60, 30)        button:setFillColor(0,0,0)     getButton = button     scrollView:insert(button)         lastTappedCard = self     button:addEventListener("tap",cardRemove) end for index = 1, #cards do   local card = display.newRect(sceneGroup,cards[index].x,cards[index].y,cards[index].w,cards[index].h)   card.anchorX = 0   card.anchorY = 0   card:setFillColor( 1, 0, 0 )   card.tap = tap   card:addEventListener( "tap" )   scrollView:insert( card )   myCardsRef[card] = card end end 

:huh: someone?

On first glance, is it possible that as well as tapping the button, which is then destroyed, the tap also permeates down to the card underneath, triggering the tap function which then redraws the button in exactly the same place?

when I put the listener type to “touch” I could see that this is the same, how can I solve this?

If you put a print statement in the ‘tap’ function, does this fire when you tap the button? Just to confirm this is the issue…

Yes! shows on the console the impression

Hi jdsmedeirosbr,

After you destroy button you create new one so it seems nothing happened.  This is duetap/touch event propagating work in Corona.  Modified code you find below

From Corona documentation:

Tap and touch events propagate until they are “handled.” This means that if you have multiple objects overlaying each other in the display hierarchy, and a tap or touch event listener has been applied to each, the event will propagate through all of these objects. However, you can stop propagation to the next underlying object by telling Corona that the event has been handled. This is as simple as returning true from the event listener — this stops the propagation cycle and prevents any underlying objects from responding to the hit event.

local mFloor = math.floor local oldButton function scene:create( event ) local sceneGroup = self.view local cards, myCardsRef = {}, {}  -- Create ScrollView scrollView = widget.newScrollView(     { top                      = display.contentHeight - display.contentHeight \* 55 / 100, left                     = 0, width                    = display.contentWidth, height                   = display.contentHeight \* 55 / 100, scrollWidth              = 600, horizontalScrollDisabled = true, scrollHeight             = 1300, listener                 = scrollListener     } )  -- Add information for each card  for i=1, 86 do     cards[#cards + 1] = {          name = 'card'..i..'.png',          x    = 8 + ( i - 1 ) % 4 \* 76,         y    = 8 + ( mFloor( ( i - 0.5 ) / 4 ) \* 98 ),         w    = ( display.contentWidth - 40 ) / 4,         h    = 90     } end  -- Tap listener for button local function cardRemove( self, event )                               display.remove( self )     oldButton = nil     print( "Toquei no Botão!" )     print( self ) -- Return nil.     return true        end -- Tap listener for card local function tap( self, event ) print( 'Tap for card' )     if lastTappedCard then         myCardsRef[lastTappedCard].alpha = 1          if oldButton and oldButton.removeSelf then         display.remove( oldButton )         end         oldButton = nil     end     myCardsRef[self].alpha = 0.5        oldButton = display.newRect( sceneGroup,myCardsRef[self].x + myCardsRef[self].width \* .5,myCardsRef[self].y + myCardsRef[self].height \* .5, 60, 30)        oldButton:setFillColor(0,0,0)     getButton = oldButton     scrollView:insert( oldButton )         lastTappedCard = self     oldButton.tap = cardRemove     oldButton:addEventListener( "tap" )     return true end for index = 1, #cards do   local card = display.newRect(sceneGroup,cards[index].x,cards[index].y,cards[index].w,cards[index].h)   card.anchorX = 0   card.anchorY = 0   card:setFillColor( 1, 0, 0 )   card.tap = tap   card:addEventListener( "tap" )   scrollView:insert( card )   myCardsRef[card] = card end sceneGroup:insert( scrollView ) end

Have a nice day:)

ldurniat

It turned out perfect! I never imagined that this multi-touch function would work like this.

Thank you very much.

:huh: someone?

On first glance, is it possible that as well as tapping the button, which is then destroyed, the tap also permeates down to the card underneath, triggering the tap function which then redraws the button in exactly the same place?

when I put the listener type to “touch” I could see that this is the same, how can I solve this?

If you put a print statement in the ‘tap’ function, does this fire when you tap the button? Just to confirm this is the issue…

Yes! shows on the console the impression

Hi jdsmedeirosbr,

After you destroy button you create new one so it seems nothing happened.  This is duetap/touch event propagating work in Corona.  Modified code you find below

From Corona documentation:

Tap and touch events propagate until they are “handled.” This means that if you have multiple objects overlaying each other in the display hierarchy, and a tap or touch event listener has been applied to each, the event will propagate through all of these objects. However, you can stop propagation to the next underlying object by telling Corona that the event has been handled. This is as simple as returning true from the event listener — this stops the propagation cycle and prevents any underlying objects from responding to the hit event.

local mFloor = math.floor local oldButton function scene:create( event ) local sceneGroup = self.view local cards, myCardsRef = {}, {}  -- Create ScrollView scrollView = widget.newScrollView(     { top                      = display.contentHeight - display.contentHeight \* 55 / 100, left                     = 0, width                    = display.contentWidth, height                   = display.contentHeight \* 55 / 100, scrollWidth              = 600, horizontalScrollDisabled = true, scrollHeight             = 1300, listener                 = scrollListener     } )  -- Add information for each card  for i=1, 86 do     cards[#cards + 1] = {          name = 'card'..i..'.png',          x    = 8 + ( i - 1 ) % 4 \* 76,         y    = 8 + ( mFloor( ( i - 0.5 ) / 4 ) \* 98 ),         w    = ( display.contentWidth - 40 ) / 4,         h    = 90     } end  -- Tap listener for button local function cardRemove( self, event )                               display.remove( self )     oldButton = nil     print( "Toquei no Botão!" )     print( self ) -- Return nil.     return true        end -- Tap listener for card local function tap( self, event ) print( 'Tap for card' )     if lastTappedCard then         myCardsRef[lastTappedCard].alpha = 1          if oldButton and oldButton.removeSelf then         display.remove( oldButton )         end         oldButton = nil     end     myCardsRef[self].alpha = 0.5        oldButton = display.newRect( sceneGroup,myCardsRef[self].x + myCardsRef[self].width \* .5,myCardsRef[self].y + myCardsRef[self].height \* .5, 60, 30)        oldButton:setFillColor(0,0,0)     getButton = oldButton     scrollView:insert( oldButton )         lastTappedCard = self     oldButton.tap = cardRemove     oldButton:addEventListener( "tap" )     return true end for index = 1, #cards do   local card = display.newRect(sceneGroup,cards[index].x,cards[index].y,cards[index].w,cards[index].h)   card.anchorX = 0   card.anchorY = 0   card:setFillColor( 1, 0, 0 )   card.tap = tap   card:addEventListener( "tap" )   scrollView:insert( card )   myCardsRef[card] = card end sceneGroup:insert( scrollView ) end

Have a nice day:)

ldurniat

It turned out perfect! I never imagined that this multi-touch function would work like this.

Thank you very much.