touch event propagation

Hi guys, I did a similar test like ChRu here
http://developer.anscamobile.com/forum/2010/10/31/swap-depth-order

It is a basic test, where I have 3 dragable objects, that can overlap each other.
I made the group:insert() work on a touch event, so the touched object is always in the foreground.
The problem that I have is that when the 3 objects are on top of each other, it seems that always the bottom one gets picked. Instead I want to have the top one to be picked. As far as I understood, the touch event always is supposed to pick the top object (as described here http://developer.anscamobile.com/content/application-programming-guide-event-handling).

here is the code of my a test:

  
object01 = display.newRect(100,100,50,50)  
object01 :setFillColor(255,0,0)  
  
object02 = display.newRect(60,100,50,50)  
object02 :setFillColor(0,255,0)  
  
object03 = display.newRect(60,100,50,50)  
object03 :setFillColor(0,0,255)  
  
group1 = display.newGroup()  
group1:insert(object01)  
group1:insert(object02)  
group1:insert(object03)  
  
eventoffset = {x=0,y=0}  
  
---------------- drag function  
  
local function dragging (event)  
 local stage = display.getCurrentStage()  
 local target = event.target  
  
  
 if event.phase == "began" then -- on touch  
 stage:setFocus(target, event.id)  
 eventoffset.x = event.x - target.x  
 eventoffset.y = event.y - target.y  
 group1:insert(target)  
 end  
  
 if event.phase == "moved" then -- on move  
 target.x = event.x - eventoffset.x  
 target.y = event.y - eventoffset.y  
 end  
  
 if event.phase == "ended" or event.phase == "cancelled" then -- on release  
 stage:setFocus(nil)  
  
 end  
  
  
end  
------------------  
object01:addEventListener("touch", dragging)  
object02:addEventListener("touch", dragging)  
object03:addEventListener("touch", dragging)  
  

Am I doing somethng wrong ? [import]uid: 10177 topic_id: 3269 reply_id: 303269[/import]

I’ve the same problem. Is this a bug? [import]uid: 10820 topic_id: 3269 reply_id: 10086[/import]

In your code you don’t “swallow” the taps, so the hit is firstly handled from the top object, then from the middle and finally from the bottom one. So, you end up dragging the last object which was on the bottom.

You have to terminate propagation of hits. Read the documentation on handling taps and coping with propagation. I can post some code here after some time (at work now) if you still have problems with that.

[import]uid: 7356 topic_id: 3269 reply_id: 10088[/import]

thanks for the reply,
I think I know now what to do. I’ll try it out later today when I’m home :wink: [import]uid: 10177 topic_id: 3269 reply_id: 10095[/import]

thanks. I’ve just tested it and it works. :smiley: [import]uid: 10820 topic_id: 3269 reply_id: 10115[/import]

Great!

[import]uid: 7356 topic_id: 3269 reply_id: 10124[/import]