Tymski Posted July 1, 2017 Share Posted July 1, 2017 I'm debugging memory leaks in my pixi application. And i stumbled on this beauty: sprite = new PIXI.Sprite(); sprite.on('pointerdown',function(){}); sprite.off(); And: sprite._events >> Object { pointerdown: Object } sprite._events has pointerdown event listener even after I deleted it with off(). Why??? Quote Link to comment Share on other sites More sharing options...
Tymski Posted July 1, 2017 Author Share Posted July 1, 2017 Quote Calling .off() with no arguments removes all handlers attached to the elements. but it doesn't work. Found this instead: sprite.removeAllListeners(); which works. ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
alex_h Posted July 1, 2017 Share Posted July 1, 2017 .off has the same signature as .on does. So you have to pass the event id and a reference to the same function instance that you passed to the .on function. In your example code that would not even be possible because you have used an anonymous function which you defined within the call to .on itself. If you want to remove the event listener later then you definitely don't want to use that approach! You need to define the listener function elsewhere and have a way of referencing it either by name or through assigning it to a variable. Then pass that reference to both the .on and the .off calls. Pixi uses EventEmitter3 for event handling so you can easily check the source code to confirm that this is the way it works: https://github.com/primus/eventemitter3/blob/master/index.js#L236 So your example code needs to be like this: const sprite = new PIXI.Sprite(); const eventHandler = function(){}; sprite.on('pointerdown', eventHandler); sprite.off('pointerdown', eventHandler); ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
Tymski Posted July 1, 2017 Author Share Posted July 1, 2017 ok I confused with jquery .on() and .off() and assumed it works the same Quote Link to comment Share on other sites More sharing options...
xerver Posted July 1, 2017 Share Posted July 1, 2017 5 hours ago, Tymski said: ok I confused with jquery .on() and .off() and assumed it works the same It is modeled after the node event emitter system, not the DOM event system or jQuery's event system. Quote Link to comment Share on other sites More sharing options...
Tymski Posted July 1, 2017 Author Share Posted July 1, 2017 Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.