Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
What is the best way to stop mouse/touch events for awhile? — Gideros Forum

What is the best way to stop mouse/touch events for awhile?

scurvyratscurvyrat Member
edited September 2012 in General questions
I want to block all mouse/touch events to my interface while I complete some network communications. It may take a second or two for the comms to complete and I don't want to launch another communication until that last one completes.

I tried an invisible rectangle, but I must be doing something wrong since the bitmap continues to get clicks when i move it offscreen:
blockRect = (Bitmap.new(Texture.new("images/transparentBlock.png")))
self:addChild(blockRect)
blockRect:setX(1500)))
blockRect:addEventListener(Event.MOUSE_DOWN, blockClicks,e)
 
function blockClicks(e)
	print(e.x,e.y,e.target,e.type)
	e:stopPropagation()
end
Why is the blockRect still receiving MOUSE_DOWN events when it isn't on screen?

Comments

  • bowerandybowerandy Guru
    edited September 2012
    Hi @scurvyrat, I do this sort of thing in my BhPopup class.

    Try adding the following extension methods to Sprite:
    function Sprite:ignoreTouchHandler(event)
    	event:stopPropagation()
    end
     
    function Sprite:ignoreTouches()
    	-- Tell a sprite to ignore (and block) all mouse and touch events
    	self:addEventListener(Event.MOUSE_DOWN, self.ignoreTouchHandler, self)
            self:addEventListener(Event.MOUSE_MOVE, self.ignoreTouchHandler, self)
            self:addEventListener(Event.MOUSE_UP, self.ignoreTouchHandler, self)
    	self:addEventListener(Event.TOUCHES_BEGIN, self.ignoreTouchHandler, self)
    	self:addEventListener(Event.TOUCHES_MOVE, self.ignoreTouchHandler, self)
    	self:addEventListener(Event.TOUCHES_END, self.ignoreTouchHandler, self)
    	self:addEventListener(Event.TOUCHES_CANCEL, self.ignoreTouchHandler, self)
    end
    and then, when you want things to go quiet do this:
    local shield=Sprite.new()
    shield:ignoreTouches()
    stage:addChild(shield)
    to restore:
    shield:removeFromParent()
    best regards
  • scurvyratscurvyrat Member
    edited September 2012
    thanks! For the definition of the shield sprite, you don't need to define its size or does it default to the full screen width/height?

    I thought it might be faster to move the sprite's X value as opposed to creating/destroying it. Does anyone know if that is true?
  • bowerandybowerandy Guru
    Accepted Answer
    Hi @scurvyrat, the shield sprite is empty. You don't need to give the shield sprite a size or any contents. Providing it is at the top of the stage z-order (and it is because you created it last) then it will receive the events before any other objects.

    Since the ignoreTouchHandler() function stops the event propagation for ALL the events it gets, whether or not they are within the shield sprite bounds or not, then it will block all the events from any of the other objects.

    If you want to see the effect in action, download that BhPopup class from the link above.

    Best regards
  • zaniarzaniar Member
    Accepted Answer
    thanks! For the definition of the shield sprite, you don't need to define its size or does it default to the full screen width/height?

    I thought it might be faster to move the sprite's X value as opposed to creating/destroying it. Does anyone know if that is true?
    Sprite doesn't have size property. But, I think, you can think that Sprite have infinite width and height.
    So, move the Sprite won't work.

    I tried an invisible rectangle, but I must be doing something wrong since the bitmap continues to get clicks when i move it offscreen:
    blockRect = (Bitmap.new(Texture.new("images/transparentBlock.png")))
    self:addChild(blockRect)
    blockRect:setX(1500)))
    blockRect:addEventListener(Event.MOUSE_DOWN, blockClicks,e)
     
    function blockClicks(e)
    	print(e.x,e.y,e.target,e.type)
    	e:stopPropagation()
    end
    Why is the blockRect still receiving MOUSE_DOWN events when it isn't on screen?
    The bitmap continues to get click event because all mouse events always received by all Sprite (including it's descentdant e.g. Bitmap) whereever the mouse events occur. In your case, you can filter mouse event position by using Sprite:hitTestPoint
    blockRect = (Bitmap.new(Texture.new("images/transparentBlock.png")))
    self:addChild(blockRect)
    blockRect:setX(1500)))
    blockRect:addEventListener(Event.MOUSE_DOWN, blockClicks,e)
     
    function blockClicks(e)
    	print(e.x,e.y,e.target,e.type)
    	if blockRect:hitTestPoint(e.x, e.y) then
    		e:stopPropagation()
    	end
    end
Sign In or Register to comment.