Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Listeners of two different objects are mixing
Started by DevilDev Aug 14 2018 10:39 PM

3 replies to this topic
event touch objects
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

DevilDev

[GLOBAL: userInfoPane.html]
DevilDev
  • Enthusiast

  • 66 posts
  • Corona SDK

When I touch  an object,listener of  the object behind it is called along with the listener of current object.

How to resolve this?

local rect=display.newRect(0,0,1000,1000);
rect:setFillColor(1,0,0);

local function r(event)
	print("rect")
end
rect:addEventListener("touch",r)


local rect2=display.newRect(0,0,200,200)
rect2:setFillColor(0,1,1)

local function r2(event)
	print("rect2")
end

rect2:addEventListener("touch",r2)



[TOPIC: post.html]
#2

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 429 posts
  • Corona SDK

That's because the touch event is propagating down through the objects.

Adding a return true to your listeners will solve this eg.

local function r(event)
  print("rect")
  return true
end

See the docs for more information... touch propagation



[TOPIC: post.html]
#3

DevilDev

[GLOBAL: userInfoPane.html]
DevilDev
  • Enthusiast

  • 66 posts
  • Corona SDK

That's because the touch event is propagating down through the objects.

Adding a return true to your listeners will solve this eg.

local function r(event)
  print("rect")
  return true
end

See the docs for more information... touch propagation

rect is my background image.

rect2 is a button..

The problem still exists

local rect=display.newRect(0,0,1000,1000);
rect:setFillColor(1,0,0);
local widget=require"widget"
local function r(event)
	print("rect")
	return true
end
rect:addEventListener("tap",r)


local function r2()
	print("button")
-- return true
end

local rect2=widget.newButton(
    {
        width = 240,
        height = 120,
        defaultFile = "start.png",
        overFile = "stop.png",
        label = "button",
        onRelease = r2
    } 
)
 

Console output when i clicked the button

 

11:58:07.475  button
11:58:07.475  rect
 
 
I changed the return true for both functions.Still same problem.What to do??


[TOPIC: post.html]
#4

XeduR @Spyric

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

  • 620 posts
  • Corona SDK

@DevilDev,

Your problem lies in using: rect:addEventListener("tap",r).

Unless I am mistaken, tap events go through all display objects in the event location. If you press and hold your button, move your mouse/finger a few pixels and then let go, then the tap event will not fire but instead the touch event activates. This is because if you just click on the button, that action is registered both as a tap and as a touch event. Tap events are sort of like touch events where the event begins and ends without entering the moving phase at any point.

 

If you change the tap event to a touch event and then include the "return true" as Appletreeman suggested, then it'll work.

 




[topic_controls]
[/topic_controls]