help with removing an event listener

Hi,

I am using composer and the middle class library.

I have the following class

local Character = class('Character') function Character:initialize(view, params) self.\_view = view self:AddEventListeners() end function Character:FlyEvent(event) print('fly') end function Character:DestroyEvent(event) self:RemoveEventListeners() end function Character:AddEventListeners() self.\_view:addEventListener("screenTouched", function(e) self:FlyEvent(e) end) end function Character:RemoveEventListeners() self.\_view:removeEventListener("screenTouched", function(e) self:FlyEvent(e) end) end return Character

i am creating a character object trhough the composer scene:create

the issue I am having is that the RemoveEventListners method is fired, but it doesn’t remove the eventlistner

any ideas? thoughts?

You are creating an anonymous function in addEventListener and removeEventListener thus this functions to Lua are two different functions. Try setting the function to a local variable (global in module) and using that or set a table listener on you object. Something like this:

function listener:

local function flyEvent(event) Character:FlyEvent(event) end function Character:AddEventListeners() self.\_view:addEventListener("screenTouched", flyEvent) end function Character:RemoveEventListeners() self.\_view:removeEventListener("screenTouched", flyEvent) end

table listener:

function Character:screenTouched(event) self:FlyEvent(event) end function Character:AddEventListeners() self.\_view:addEventListener("screenTouched", self) end function Character:RemoveEventListeners() self.\_view:removeEventListener("screenTouched", self) end

Code not checked. Check for errors or typos.

 

primoz.cerar,

Thanks for that, makes sense… so does that mean for things like collisions as well can I do the same? As it is a Corona generated event?

function Character:AddEventListeners() self.\_view:addEventListener("collision", self) end

or do I need to do that differently?

If you have function Character:collision declared then yes you can do that.

primoz,

Interestingly when I do the following

 self.\_character:addEventListener("collision", function(e) self:CollisionHandler(e) end )

and then remove with 

 self.\_character:removeEventListener("collision", function(e) self:CollisionHandler(e) end ) 

that works… how is that possible? shouldn’t this have the same issue as the others?

my function is

function Character:CollisionHandler(event) //code end

That should not work. Are you sure the addEventListener actually does anything?

I don’t know the structure of your objects.

In the above code I don’t know what self is or what self._character is.

By the code you wrote I’m guessing self is a Character instance.

What is self._character? I don’t see you setting it anywhere.

Primoz,

yeah it def works… as anytime the character collides it prints out, also when i call the remove listener it stops the collision events

the  which is what confused me

yeah self is a character instance

self._character is the actual sprite instance

 self.\_character = display.newSprite(self.\_view, imageSheet , sequenceData )

Then I don’t know how this can work. It should not work.

You must be a magician :slight_smile:

i don’t like being a magician… :frowning:

Primoz,

question for you

if I have the following method in my class

function Character:CollisionHandler(event) ..... end

How can I use a table listener to attach that to a collision event?

function Character:AddEventListeners() self.\_view:addEventListener("collision", self) --???? what would I put here? end

Thanks

Every time you want to make a table listener, the table has to have a method with the same name as the event. In this case you would have to call it function Character:collision(event). Then it will work with the addEventListener line you have now.

You are creating an anonymous function in addEventListener and removeEventListener thus this functions to Lua are two different functions. Try setting the function to a local variable (global in module) and using that or set a table listener on you object. Something like this:

function listener:

local function flyEvent(event) Character:FlyEvent(event) end function Character:AddEventListeners() self.\_view:addEventListener("screenTouched", flyEvent) end function Character:RemoveEventListeners() self.\_view:removeEventListener("screenTouched", flyEvent) end

table listener:

function Character:screenTouched(event) self:FlyEvent(event) end function Character:AddEventListeners() self.\_view:addEventListener("screenTouched", self) end function Character:RemoveEventListeners() self.\_view:removeEventListener("screenTouched", self) end

Code not checked. Check for errors or typos.

 

primoz.cerar,

Thanks for that, makes sense… so does that mean for things like collisions as well can I do the same? As it is a Corona generated event?

function Character:AddEventListeners() self.\_view:addEventListener("collision", self) end

or do I need to do that differently?

If you have function Character:collision declared then yes you can do that.

primoz,

Interestingly when I do the following

 self.\_character:addEventListener("collision", function(e) self:CollisionHandler(e) end )

and then remove with 

 self.\_character:removeEventListener("collision", function(e) self:CollisionHandler(e) end ) 

that works… how is that possible? shouldn’t this have the same issue as the others?

my function is

function Character:CollisionHandler(event) //code end

That should not work. Are you sure the addEventListener actually does anything?

I don’t know the structure of your objects.

In the above code I don’t know what self is or what self._character is.

By the code you wrote I’m guessing self is a Character instance.

What is self._character? I don’t see you setting it anywhere.

Primoz,

yeah it def works… as anytime the character collides it prints out, also when i call the remove listener it stops the collision events

the  which is what confused me

yeah self is a character instance

self._character is the actual sprite instance

 self.\_character = display.newSprite(self.\_view, imageSheet , sequenceData )

Then I don’t know how this can work. It should not work.

You must be a magician :slight_smile:

i don’t like being a magician… :frowning:

Primoz,

question for you

if I have the following method in my class

function Character:CollisionHandler(event) ..... end

How can I use a table listener to attach that to a collision event?

function Character:AddEventListeners() self.\_view:addEventListener("collision", self) --???? what would I put here? end

Thanks