Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How could remove event listener properly — Gideros Forum

How could remove event listener properly

RickyngkRickyngk Member
edited April 2012 in General questions
Hi,

For quick implement, sometimes I use addEventListener as following:
mySprite:addEventListener("onMyEvent", 
	function(event)
		print("active my event")
	end
)
However, I don't know how to remove that event listener, because I can't get "listener" value.
EventDispatcher:removeEventListener(type, listener, data)
Count I remove all listeners of type, without declare listener and data?

Thanks,

Comments

  • I would actually recommend NOT using anonymous functions (or closures) in that way for that very reason.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • ar2rsawseenar2rsawseen Maintainer
    edited April 2012
    local listener = function(event)
    	print("active my event")
    end
    mySprite:addEventListener("onMyEvent", listener)
     
    mySprite:removeEventListener("onMyEvent", listener)
    You only need to be aware of scopes, if you remove listener in other scope, then you either make listener as property of bigger scope (as class, for example), or make it global.

    Likes: saeys

    +1 -1 (+1 / -0 )Share on Facebook
  • Well I could be wrong, but I do this....
    local timer = Timer.new(5000, 1)
    timer:addEventListener(Event.TIMER,
    function(event)
    --[[Do my shit here in 5 seconds]]
    end
    )
    timer:start()
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • atilimatilim Maintainer
    @Cyberience Also there is a shortcut for this:
    Timer.delayedCall(5000, function()
      -- do your stuff
    end)

    Likes: Cyberience

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