removeEventListener dosen't work in a 30log class

Hi, I just started to use 30log for OOP. I created a simple class, the removeEventListener doesn’t working
Here is the code:
 

local Character = class() Character.\_\_name = "Character" function Character:listener (obj) self.animation = false self.image.x = self.startX self.image.y = self.startY self.image:removeEventListener("touch", function(target) self:onTouch(target) end) --self.image:removeSelf() --self.image = nil end function Character:test() print (self.name) end function Character:completeMoveTo (obj) timer.performWithDelay( 1000, function(target) self:listener(obj) end) end function Character:completeMovdBy ( obj ) transition.moveTo( obj, { x= self.endX, y=self.endY, time=2000, onComplete=function(target) self:completeMoveTo(target) end } ) end function Character:onTouch(event) if ( event.phase == "began" and not self.animation) then self.image.x = self.startX self.image.y = self.startY self.animation = true transition.moveBy( self.image, { x=100, y=0, time=500 , onComplete=function(target) self:completeMovdBy(target) end} ) end end function Character:init( image, name, startX, startY, endX, endY ) self.image = display.newImage( image ) self.name = name self.animation = false self.startX = startX self.startY = startY self.endX = endX self.endY = endY self.image.x = startX self.image.y = startY self.image:addEventListener("touch", function(target) self:onTouch(target) end) end return Character

Thanks in advance
Yalon

As I know you just need to save the reference to your listener to be able to remove it later.

Notice

Use 

local Character = class("Character") 

instead of 

local Character = class() Character.\_\_name = "Character"

Tried that, same result.
Can you create a small class with an event listener as an example?

Thanks

Tried to create minimal class with remove event listener and also did not work:
 

local class = require 'modules.30log' local Character2 = class("Character2") function Character2:init(image, x,y) self.image = display.newImage( image ) self.image.x = x self.image.y = y self.image:addEventListener("touch", function (target) self:onTouch(target) end) end function Character2:onTouch(event) if ( event.phase == "began" ) then print (self.image.x) self.image:removeEventListener("touch", function (target) self:onTouch(target) end) end end return Character2

Here you go

local Character = class("Character") function Character:removeListener (obj)   self.rect:removeEventListener( "touch", self.onTouch ) end function Character.onTouch(event)   print( event.phase ) end function Character:init()  self.rect = display.newRect( 100, 100, 100, 100 )  self.rect:addEventListener( "touch", self.onTouch )  timer.performWithDelay( 2000, function() self:removeListener() end) end Character()

Thanks!!

It’s working - I’ll implement this in my code

Hi again Idurnaiat :slight_smile:
Can you change the class you gave me - the remove listener will be called in the onTouch function?

Thanks

 

I finally succeeded

local Character3 = class("Character3") function Character3:removeListener (obj) self.rect:removeEventListener( "touch", self.handler) end function Character3.onTouch(self, event) print( event.phase ) self:removeListener() end function Character3:init() self.rect = display.newRect( 100, 100, 100, 100 ) self.handler = function(target) self.onTouch(self, target) end self.rect:addEventListener( "touch", self.handler ) end return Character3

The trick was to save a handler to the anonymous function

Right.  You can’t remove a generic handler.  It must be the same one you added originally.

As I know you just need to save the reference to your listener to be able to remove it later.

Notice

Use 

local Character = class("Character") 

instead of 

local Character = class() Character.\_\_name = "Character"

Tried that, same result.
Can you create a small class with an event listener as an example?

Thanks

Tried to create minimal class with remove event listener and also did not work:
 

local class = require 'modules.30log' local Character2 = class("Character2") function Character2:init(image, x,y) self.image = display.newImage( image ) self.image.x = x self.image.y = y self.image:addEventListener("touch", function (target) self:onTouch(target) end) end function Character2:onTouch(event) if ( event.phase == "began" ) then print (self.image.x) self.image:removeEventListener("touch", function (target) self:onTouch(target) end) end end return Character2

Here you go

local Character = class("Character") function Character:removeListener (obj)   self.rect:removeEventListener( "touch", self.onTouch ) end function Character.onTouch(event)   print( event.phase ) end function Character:init()  self.rect = display.newRect( 100, 100, 100, 100 )  self.rect:addEventListener( "touch", self.onTouch )  timer.performWithDelay( 2000, function() self:removeListener() end) end Character()

Thanks!!

It’s working - I’ll implement this in my code

Hi again Idurnaiat :slight_smile:
Can you change the class you gave me - the remove listener will be called in the onTouch function?

Thanks

 

I finally succeeded

local Character3 = class("Character3") function Character3:removeListener (obj) self.rect:removeEventListener( "touch", self.handler) end function Character3.onTouch(self, event) print( event.phase ) self:removeListener() end function Character3:init() self.rect = display.newRect( 100, 100, 100, 100 ) self.handler = function(target) self.onTouch(self, target) end self.rect:addEventListener( "touch", self.handler ) end return Character3

The trick was to save a handler to the anonymous function

Right.  You can’t remove a generic handler.  It must be the same one you added originally.