Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

I can not remove object
Started by jdsmedeirosbr Jul 12 2018 11:35 AM

7 replies to this topic

Best Answer ldurniat , 13 July 2018 - 04:15 AM

Hi jdsmedeirosbr,

 

After you destroy button you create new one so it seems nothing happened.  This is due tap/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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 37 posts
  • Corona SDK

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 

Attached Files



[TOPIC: post.html]
#2

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 37 posts
  • Corona SDK

:huh: someone?



[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,456 posts
  • Corona SDK

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?



[TOPIC: post.html]
#4

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 37 posts
  • Corona SDK

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?



[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,456 posts
  • Corona SDK

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...



[TOPIC: post.html]
#6

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 37 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 321 posts
  • Corona SDK

  Best Answer

Hi jdsmedeirosbr,

 

After you destroy button you create new one so it seems nothing happened.  This is due tap/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


Edited by ldurniat, 13 July 2018 - 04:17 AM.


[TOPIC: post.html]
#8

jdsmedeirosbr

[GLOBAL: userInfoPane.html]
jdsmedeirosbr
  • Enthusiast

  • 37 posts
  • Corona SDK

Hi jdsmedeirosbr,

 

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

 

From Corona documentation:

 

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.



[topic_controls]
[/topic_controls]