Remove Event Listener from class?

Hi Guys

So I created a button class which has a onButtonTouch function inside it.

Inside my scene I created the button …

local buttonA = Button:new( )

But now I’m stuck on how to remove the eventListener when I no longer need the button to be interactive.

I know how to remove an even listener when the function has been created inside the scene, but can’t think of how to do this.

Any ideas?

Thank you in advance!

Hi @Ninjadoodle,

Can you place a reference to the listener function on the actual button, then remove it “locally” using that? Another option, if the button was created using the widget library and it won’t be a big performance issue to have the listener still going, is to just use :setEnabled to turn its operation on and off.

http://docs.coronalabs.com/api/type/ButtonWidget/setEnabled.html

Take care,

Brent

Hi @Brent Sorrentino

Thank you for your help! Here is what I’ve got, but I’m not sure if it’s a good way of doing things. Not sure about the locals and globals …

This is my Button.lua class

Button = {} function Button:new(x, y) local self = display.newImageRect( "button.png", 64, 64 ) local function buttonTouch( event ) if ( event.phase == "began" ) then // do something end end self:addEventListener( "touch", buttonTouch ) function self.removeButtonTouch() self:removeEventListener( "touch", buttonTouch ) end return self end return Button

and after creating the button in the storyboard scene, I would just go …

local button = Button:new( 100, 100 ) group:insert( button ) button.removeTouch();

I’m really not sure whether this code will cause me problem in the future tho :frowning:

For example …

Button = {}
function Button:new(x, y)

Do any of these need to be local, or does it really not matter, since I create a local instance of the button inside the storyboard scene, which in turns localises the class?

Sorry if that sounds confusing, just trying to understand this :slight_smile:

you should defintely localise/localize the button table and then require it instead of just making it global, off course there are certain cases where a global is the best course of action but if it is global, you wont need to return it at the end. Oh and btw i wouldn’t recomend using “self” and “:” because the self that you are using could be overriding the self that is passed as the initial param by lua. Instead you could change self to something else or in your constructor change the “:” to a “.”

Regards, Lemon

Hi @86lemonade68

Thanks heaps for your help!

I think I’m slowly starting to get the hang of it. I figured out that for my case, a better approach was to use an external module instead of making a class. I followed the Corona ‘a better way to approach modules’ tutorial to make sure its all localised.

https://coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/

I’m still a little confused on when to use ‘.’ and ‘:’ , even after reading a couple of articles, haha :slight_smile:

Thanks again!

Hi @Ninjadoodle,

Can you place a reference to the listener function on the actual button, then remove it “locally” using that? Another option, if the button was created using the widget library and it won’t be a big performance issue to have the listener still going, is to just use :setEnabled to turn its operation on and off.

http://docs.coronalabs.com/api/type/ButtonWidget/setEnabled.html

Take care,

Brent

Hi @Brent Sorrentino

Thank you for your help! Here is what I’ve got, but I’m not sure if it’s a good way of doing things. Not sure about the locals and globals …

This is my Button.lua class

Button = {} function Button:new(x, y) local self = display.newImageRect( "button.png", 64, 64 ) local function buttonTouch( event ) if ( event.phase == "began" ) then // do something end end self:addEventListener( "touch", buttonTouch ) function self.removeButtonTouch() self:removeEventListener( "touch", buttonTouch ) end return self end return Button

and after creating the button in the storyboard scene, I would just go …

local button = Button:new( 100, 100 ) group:insert( button ) button.removeTouch();

I’m really not sure whether this code will cause me problem in the future tho :frowning:

For example …

Button = {}
function Button:new(x, y)

Do any of these need to be local, or does it really not matter, since I create a local instance of the button inside the storyboard scene, which in turns localises the class?

Sorry if that sounds confusing, just trying to understand this :slight_smile:

you should defintely localise/localize the button table and then require it instead of just making it global, off course there are certain cases where a global is the best course of action but if it is global, you wont need to return it at the end. Oh and btw i wouldn’t recomend using “self” and “:” because the self that you are using could be overriding the self that is passed as the initial param by lua. Instead you could change self to something else or in your constructor change the “:” to a “.”

Regards, Lemon

Hi @86lemonade68

Thanks heaps for your help!

I think I’m slowly starting to get the hang of it. I figured out that for my case, a better approach was to use an external module instead of making a class. I followed the Corona ‘a better way to approach modules’ tutorial to make sure its all localised.

https://coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/

I’m still a little confused on when to use ‘.’ and ‘:’ , even after reading a couple of articles, haha :slight_smile:

Thanks again!