Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Why event:stopPropagation() doesn't work for me? - Gideros Forum

Why event:stopPropagation() doesn't work for me?

Apollo14Apollo14 Member
edited August 15 in General questions
Hi guys!
I have black overlay to prevent buttons underneath it being clicked.
Why it doesn't stop event.touch propagation? It detects touches, but they propagate further below. :(
--BlackOverlay.lua:
BlackOverlay=Core.class(Sprite)
 
function BlackOverlay:init(overlayColor)
	local color=overlayColor or 0
	self.pixel=Pixel.new(color,1,720,1280); --self.pixel:setAlpha(0)
	self:addChild(self.pixel)
 
	self:addEventListener(Event.TOUCHES_END, function(event)
		if self.pixel:hitTestPoint(event.touch.x,event.touch.y) then event:stopPropagation(); print"touched pixel" end
	end)
end
--Main.lua:
sceneManager = SceneManager.new({
	["scene1"] = scene1,
	["scene2"] = scene2,
	["scene3"] = scene3,
	["scene4"] = scene4,
	["scene5"] = scene5,
})
stage:addChild(sceneManager)
sceneManager:changeScene("scene1")
 
BLACKOVERLAY=BlackOverlay.new()
stage:addChild(BLACKOVERLAY)
> Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)

Comments

  • olegoleg Member
    edited August 15
    дотики припиняються лише на ті об'єкти які нижче в ієрархії сцени, а на ті об'єкти які вище -дотики не припиняються..

    переконайся що вище спрайту нічого немає

    також додай подію нажаття(click down) і там теж пропиши event:stopPropagation
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • scenemanager is lower than black overlay, but the touch still reaches scenemanager
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • olegoleg Member
    Apollo14 said:

    scenemanager is lower than black overlay, but the touch still reaches scenemanager

    зроби змінну pause=true , і вимикай обробку подій коли вона увімкнена, -я таку обробку додав прямо у свій клас кнопок

    коли дадаєш оверлей вмикай змінну, коли прибираєш вимикай
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • @oleg such solutions will create many inconveniences later on, interface in this game is very complicated
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • piepie Member
    Go to gideros coding easy by @ar2rsawseen and copy lines from 571 to 601 somewhere in your project, then you can use sprite:ignoreTouches and sprite:enableTouches ;)

    Here: https://github.com/ar2rsawseen/GiderosCodingEasy/blob/master/GiderosCodingEasy.lua

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • @pie wow actually this whole library is pretty handy :smile:
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • hgy29hgy29 Maintainer
    Actually putting a sprite layer to stop event propagation is so common that I think gideros should have a simpler api to do it

    Likes: pie, Apollo14

    +1 -1 (+2 / -0 ) Share on Facebook
  • piepie Member
    @Apollo14 yes it is, but it's pretty old and some method could be no longer compatible with current gideros api (I am thinking about textfield additions). Plus it mess up with setters and getters and it makes it incompatible with Layers lib by nicke and if I recall with some other things too.. Something still works though, I think that the best way to use it is to extract the code you need and use just that (sprite:hide and sprite:show are pretty useful too) :)

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • antixantix Member
    edited August 15
    Maybe you should be using "TOUCHES_BEGIN" because if you are using TOUCHES_END then other objects have already received events.

    Maybe try something like this (not tested)...
    --BlackOverlay.lua:
    BlackOverlay=Core.class(Sprite)
     
    function BlackOverlay:init(overlayColor)
    	local color=overlayColor or 0
    	self.pixel=Pixel.new(color,1,720,1280); --self.pixel:setAlpha(0)
    	self:addChild(self.pixel)
     
    	self:addEventListener(Event.TOUCHES_BEGIN, self.gobbleTouches(self)
    	end)
    	self:addEventListener(Event.TOUCHES_MOVE self.gobbleTouches(self)
    	end)
    	self:addEventListener(Event.TOUCHES_END, self.gobbleTouches(self)
    	end)
     
    function BlackOverlay:gobbleTouches(e)
    	if self.pixel:hitTestPoint(e.touch.x,e.touch.y) then e:stopPropagation(); print"touched pixel"
    end
    end

    Likes: pie, Apollo14

    +1 -1 (+2 / -0 ) Share on Facebook
Sign In or Register to comment.