Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
another event listener on multiple object problem — Gideros Forum

another event listener on multiple object problem

jimlevjimlev Member
edited June 2014 in Code snippets

Sorry if the answer is somewhere on the forum but I haven't found it.

So...
I have a camera class with a touchEvent listener (to manage a drag functionality)
I attach a bunch of sprites to that camera (camera:addchid...). These sprites also have a touchEvent listener.

The result I want :
I can drag my camera to scroll through my sprites and I can click one of the sprites to activate him BUT...

when I click one of the sprites (hitTest verified and with the use of the stopPropagation), the camera Event also occurs.

any idea?
My meditation plan :
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited June 2014 Accepted Answer
    The event is propagated from children to parents
    and if you use stopPropagation, then event should not propogate any further

    As in:
    local parent = Sprite.new()
    local child1 = Sprite.new()
    local child2 = Sprite.new()
     
    parent:addChild(child1)
    parent:addChild(child2)
     
    parent:addEventListener(Event.MOUSE_DOWN, function(self, e)
        if self:hitTestPoint(e.x, e.y) then
            e:stopPropagation()
            print("parent clicked")
        end
    end, parent)
     
    child1:addEventListener(Event.MOUSE_DOWN, function(self, e)
        if self:hitTestPoint(e.x, e.y) then
            e:stopPropagation()
            print("child1 clicked")
        end
    end, child1)
     
    child2:addEventListener(Event.MOUSE_DOWN, function(self, e)
        if self:hitTestPoint(e.x, e.y) then
            e:stopPropagation()
            print("child2 clicked")
        end
    end, child2)
    In this scenario, you click on screen, first the event is raised for child1, if child1 was not hit, then child2, if child2 was not hit then parent

    If child2 was hit then event stops here and parent does not receive the event.

    This is how it should work :)

    What structure do you have?
  • jimlevjimlev Member

    Thanks ar2rsawseen ! Good to see u every time I need, just behind my shoulder :D

    My code's structure looks quite near of your example (as much as I can judge with my pitiful dev skill) :-S

    But, I have the answer I want :" It should work". So, I'm gonna try to clean a little bit my code and find, by myself, where is the problem !

    I'll be back if I doesn't found it in few hours :O
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • jimlevjimlev Member
    Ok... so... note for later...
    stopPropagation in a child's event.mouse_down doesn't stop the event's propagation
    to his parent's event.touch_down... :D ~X(
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • ar2rsawseenar2rsawseen Maintainer
    yep, that sounds like a possible mistake :)
Sign In or Register to comment.