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
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 again for your efficiency !!
first it prints the sentence "gun created"
then the function
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.
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
by the way how do you post nicely formatted and syntax colored text ?
thanks again,
Jp