Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Event listeners overlapping
Started by coreybuckley13 May 12 2019 03:37 PM

3 replies to this topic

Best Answer XeduR @Spyric , 13 May 2019 - 08:06 AM

With touch listeners, you often want to include this line of code at the very end of your function:

return true

By including this bit, the touch listener will block the touch event from interacting with other objects below it (i.e. you stop the touch event propagation, which seems to currently be occurring).

Also, you don't need to include this bit "
background.isHitTestable = true" if an object is visible. That is only needed if you have an object that is invisible, but you still want to be able to touch it.
 

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

coreybuckley13

[GLOBAL: userInfoPane.html]
coreybuckley13
  • Observer

  • 2 posts
  • Corona SDK

I have a function to create a box where the user clicks, and when the user clicks on that same box I want it to be removed from the display. My problem is the box gets removed and then created again because the two events are happening one after another.

 

I have the createBox function bound to the "tap" event on the background and the "tap" event on the box bound to an anonymous function that removes the box.

 

I'm pretty new to corona, suggestions on how to best handle this is much appreciated!

 

local width = 100
local physics = require("physics")
physics.start()


uiGroup = display.newGroup()


background = display.newRect(uiGroup, display.contentCenterX, display.contentCenterY, display.contentWidth, display.actualContentHeight)
background:setFillColor(1,0,0)
background.alpha = 1
background.isHitTestable = true
background:toBack()


local function createBox(event)
    little_rect = display.newRect(uiGroup, event.x, event.y, width/4, width/4)
    physics.addBody(little_rect, "static")
    print("created box")
    little_rect:addEventListener("tap", function(event)
        print("removed box")
        display.remove(little_rect)
    end)
end




background:addEventListener("tap", createBox)

 



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 838 posts
  • Corona SDK

  Best Answer

With touch listeners, you often want to include this line of code at the very end of your function:

return true

By including this bit, the touch listener will block the touch event from interacting with other objects below it (i.e. you stop the touch event propagation, which seems to currently be occurring).

Also, you don't need to include this bit "
background.isHitTestable = true" if an object is visible. That is only needed if you have an object that is invisible, but you still want to be able to touch it.
 


  • coreybuckley13 likes this

[TOPIC: post.html]
#3

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

The other issue is all the boxes are created with the same global variable little_rect. When a box is tapped, the last box gets removed instead of the one that's being tapped on. Change the line to this will fix it:

local little_rect = display.newRect(uiGroup, event.x, event.y, width/4, width/4)

BTW, you should also change other variables to local variables unless you really need them to be global.

 

Dave


  • coreybuckley13 likes this

[TOPIC: post.html]
#4

coreybuckley13

[GLOBAL: userInfoPane.html]
coreybuckley13
  • Observer

  • 2 posts
  • Corona SDK

Thank you all, your comments have been very helpful and I've solved my issue!  :)




[topic_controls]
[/topic_controls]