Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
stuck with simple custom event — Gideros Forum

stuck with simple custom event

jalmincejalmince Member
edited January 2014 in General questions
Hi there! Hope somebody can help, or explain.

I stripped down my code to the basic following and still can' t understand what is wrong:

object 1 ( class Gun) sends a custom event ("shoot"), object 2 ( class target ) never gets it ( at least the way I am coding it..)

---------------------( gun.lua ) -------
Gun = gideros.class(Sprite)

function Gun:init()
print( "gun created" )
function Gun:onMouseDown()
self:dispatchEvent(Event.new("shoot"), self)
print( " shoot dispatched ")
end
self:addEventListener( Event.MOUSE_DOWN, self.onMouseDown, self)
end

-------------------------( target.lua )------------
Target = gideros.class(Sprite)

function Target:init()
self:addEventListener( "shoot", function() print("got it") end)
print( " Target created, listening to shoots")
end

-------------------(main.lua) -------------
target = Target.new()
gun = Gun.new()

stage:addChild( gun)
stage:addChild( target)
--------------------------------------

In the output window, I get:

Uploading finished.
Target created, listening to shoots
Gun created
shoot dispatched
shoot dispatched
shoot dispatched
....
but never see "got it" from the event listener ??

thanks in advance if somebody can sort me out !

JP

Comments

  • @jalmince, could it be that you have the listener on Target but are dispatching the events to Gun, so Target never receives the events.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @jalmince yes the objects need to have something in common, for example the scene they are on
    Gun = Core.class(Sprite)
     
    function Gun:init(scene) 
    	print( "gun created" )
    	scene:dispatchEvent(Event.new("shoot"), self) 
    end
     
    Target = Core.class(Sprite)
     
    function Target:init(scene)
    	scene:addEventListener( "shoot", function() print("got it") end)
    	print( " Target created, listening to shoots")
    end
     
    Scene = Core.class(Sprite)
     
    function Scene:init()
    	target = Target.new(self)
    	gun = Gun.new(self)
     
    	self:addChild(gun)
    	self:addChild(target)
    end
     
    stage:addChild(Scene.new())
    oor you can always hack your way out of it, like this:
    allEventListeners = setmetatable({}, {__mode = "v"})
     
    function broadcastEvent(event)
    	for k in pairs(allEventListeners) do
    		k:dispatchEvent(event)
    	end
    end
     
     
    Gun = Core.class(Sprite)
     
    function Gun:init() 
    	print( "gun created" )
    	broadcastEvent(Event.new("shoot")) 
    end
     
    Target = Core.class(Sprite)
     
    function Target:init()
    	allEventListeners[self] = true
    	self:addEventListener( "shoot", function() print("got it") end)
    	print( " Target created, listening to shoots")
    end
     
     
    target = Target.new()
    gun = Gun.new()
  • @ar2rsawseen : many thanks. Think I get your point: when an object 1 wants to send an event to an object2, the event has to be dispatched to higher up in the hierarchy containing object2 right ?

    Many thanks again for your efficiency !!
  • @jalmince,
    ---------------------( gun.lua ) -------
    Gun = gideros.class(Sprite)
     
    function Gun:init()
      print( "gun created" )
     
      function Gun:onMouseDown()
        self:dispatchEvent(Event.new("shoot"), self)
        print( " shoot dispatched ")
      end
     
      self:addEventListener( Event.MOUSE_DOWN, self.onMouseDown, self)
    end
     
    -------------------------( target.lua )------------
    Target = gideros.class(Sprite)
     
    function Target:init()
      self:addEventListener( "shoot", function() print("got it") end)
      print( " Target created, listening to shoots")
    end
    If you are looking for a detailed understanding, then look at the
    gun.lua
    the function
    Gun:init
    works as follows

    first it prints the sentence "gun created"

    then the function
    Gun:onMouseDown()
    dispatches an event called shoot and prints "shoot dispatched". This is where you need to look carefully. Dispatch is used as
    self:dispatchEvent(Event.new("shoot"), self)
    The self in this function is the same as Gun, so essentially what you are using is,
    Gun:dispatchEvent(Event.new("shoot"), Gun)
    therefore the event is send to the Gun object where as you are expecting the same to be captured by another object.

    Now with the Target object, when it is created, it sets the event listener to listen for the custom event "shoot"

    What happens as I suggested earlier is that Target never gets the event shoot as the custom event shoot is dispatched to the Gun object not Target.

    You can as suggested by @ar2sawseen have a common object that captures and dispatches custom events. One way would be to use the stage object or the Scene as suggested by @ar2rsawseen.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Many thanks, I got it now ..
    by the way how do you post nicely formatted and syntax colored text ?
    thanks again,
    Jp
Sign In or Register to comment.