I've seen this mentioned before and found a thread from last December about it (here:
http://www.giderosmobile.com/forum/discussion/287/suggestion-for-custom-events#Item_1 ) but are custom broadcast events possible yet? By that I mean an event that goes to all objects that have a listener for it, rather than just a specific object?
I think I've got a way around it by getting my sub-objects to register a listener on the main parent that receives the event. I've done this by something like the following in the constructor for my objects:
<pre>
function Blah:init()
function stageAdded(event)
gamespace:addEventListener("game_paused", pauseGame)
gamespace:addEventListener("game_resumed", resumeGame)
end
function stageRemoved(event)
gamespace:removeEventListener("game_paused", pauseGame)
gamespace:removeEventListener("game_resumed", resumeGame)
end
self:addEventListener(Event.ADDED_TO_STAGE, stageAdded)
self:addEventListener(Event.REMOVED_FROM_STAGE, stageRemoved)
end |
This appears to work (although I haven't tested it fully) but it's a bit of a hack really because gamespace (in this example) is global.
Comments
I don't have much time to answer but there were a couple of suggestions that people made on that thread :
Broadcast messages for custom events : how to?.
That's the result I got : Broadcast messages - result
I hope you will find some things that will help you there.
I think there's a problem with my solution though as I'm getting periodic crashes caused by the event dispatcher dispatching an event to an object that's no longer a child. Going to debug that this morning to see what's going on. As I'm removing all children from the scene before destroying them, first place to check is that Event.REMOVED_FROM_STAGE is actually triggering 100% of the time.
Likes: Mells, moopf
I learned something new today.
For those who want to have a better understanding of the solution provided by atilim :
Weak Tables Tutorial - at lua-users.org
Likes: moopf
Likes: atilim