Simple group question

Hi,

  1. I am having a group that covers the whole screen.

  2. On top of that group I have a smaller group and it is positioned in a fix place.

I want the first group to react on touches, but not on the smaller group.

Any ideas on how to do this?

Joakim [import]uid: 81188 topic_id: 22426 reply_id: 322426[/import]

You set up a touch handler on the smaller group and have it return “false”.

local bigGroup = display.newGroup()  
...  
  
local function bigGroupTouch(event)  
 -- do your stuff here  
end  
bigGroup:addEventListener("touch", bigGroupTouch)  
  
local smallGroup = display.newGroup()  
...  
  
local function smallGroupTouch(event)  
 return false  
end  
smallGroup:addEventListener("touch", smallGroupTouch)  
  

This is known as event propagation or event bubbling. LittleGroup will still receive the touch event, but since you are returning false, it tells the system that you didn’t handle it and it needs to bubble up (well down in visual order) to the next thing in the display hirearchy. bigGroup will then get the event and can do what it needs to do.

[import]uid: 19626 topic_id: 22426 reply_id: 89462[/import]

I think Joakims problem is that the background group reacts to touches, even when the smaller foreground group was touched.

To prevent that, you have to stop the event bubbling up by returning true.

sample:

local group1 = display.newGroup()   
local group2 = display.newGroup()   
myObject1 = display.newRect( 10, 10, 200, 200 )   
myObject1:setFillColor ( 100, 100, 100 )  
myObject2 = display.newRect( 100, 100, 100, 100 )   
group1:insert(myObject1)  
group2:insert(myObject2)  
   
function doOne(event)  
 local t = event.target  
 if ( event.phase == "ended" ) then  
 print ("Group 1")   
 end   
end  
  
function doTwo(event)  
 local t = event.target   
 if ( event.phase == "ended" ) then  
 print ("Group 2")   
 end   
 return true -- if !true, bubbling up occurs  
end  
  
group1:addEventListener("touch", doOne)  
group2:addEventListener("touch", doTwo)  

-finefin [import]uid: 70635 topic_id: 22426 reply_id: 89477[/import]

I read it that he wanted the background group to get touches and the foreground group to not get them. Did I read that wrong? [import]uid: 19626 topic_id: 22426 reply_id: 89481[/import]