Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
EventDispatcher:addEventListener(type, listener, data) — Gideros Forum

EventDispatcher:addEventListener(type, listener, data)

GregBUGGregBUG Guru
edited April 2012 in Building a team?
Sorry but i can't find a solution...

but a function defined as "listener" can be part of a class function and can call another class function?

... yes i'm not very clear ...

try to explain with some "stripped" code example:
-- my class 
CTNTVirtualPad = Core.class(Sprite)
 
-- called by listener functions... but got and error
function CTNTVirtualPad:checkPad(event)
   print(event)
end
 
 
--- LISTENER FUNCTIONS ----
-----------------------
-- Touches begin event --
-----------------------
function CTNTVirtualPad:onTouchesBegin(event)
	self:checkPad(Event.TOUCHES_BEGIN)
end
 
-----------------------
-- Touches move event --
-----------------------
function CTNTVirtualPad:onTouchesMove(event)
	self:checkPad(Event.TOUCHES_MOVE)
end
 
-----------------------
-- Touches end event --
-----------------------
function CTNTVirtualPad:onTouchesEnd(event)
	self:checkPad(Event.TOUCHES_END)
end
 
--- DEFINE EVENT LISTENER
-----------------------
-- start Virutal Pad and events --
-----------------------
function CTNTVirtualPad:start()
  stage:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin)
  stage:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove)
  stage:addEventListener(Event.TOUCHES_END, self.onTouchesEnd)
end
when you try to touch the screen of device you get this error:

tntvirtualpad.lua:204: attempt to call method 'checkPad' (a nil value)
stack traceback:
tntvirtualpad.lua:204: in function

but checkPad is not null!!

ahhhhrrrrgggg!

the event is raised correctly but seems that my function checkPad is not defined (nil)

why? :((

EDIT:

obviously if checkPad is defined as "local function" the code works fine...

ex:
local function checkPad(event)
  print(event)
end
:-?
TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
www.tntengine.com

Comments

  • chipster123chipster123 Member
    edited April 2012
    I think you need the additional argument 'self' to the addEventListener() call.
     
    --- DEFINE EVENT LISTENER
    -----------------------
    -- start Virutal Pad and events --
    -----------------------
    function CTNTVirtualPad:start()
      stage:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
      stage:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
      stage:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
    end

    Likes: Averett

    +1 -1 (+1 / -0 )Share on Facebook
  • doh! yes!

    thanks chipster123!!!

    i lost some hours around this error...

    but my eyes not see this *BIG* and *stupid* code error...

    mmm it's better i go to sleep!! doh!!!

    thanks again chipster!
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • I keep making the mistake of typing stage:addEventListener(Event.TOUCHES_BEGIN, Object:onTouchesBegin) and produce similar errors.  Very aggravating but after doing it enough times I think I've learned to not do it anymore.
  • :P
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • petecpetec Member
    I spent most of yesterday doing exactly the same with self.world:addEventListener(Event.BEGIN_CONTACT,self.onBeginContact)
    and driving myself crazy. I was very glad to see @chipster123's post above (thanks!).

    I just hope I remember next time I need it. Note to self....
Sign In or Register to comment.