Hi Everyone!
Excuse the cryptic title...
I have been using enter_frame as my "game loop" and decided to try replacing it with a custom event that is called at regular intervals (every 50ms, 20fps) to give a regular loop. I was going to benchmark these two methods (custom events vs enter_frame) once i had them both working.
I created a new class that was basically empty, but had a timer that fired a custom event (say, game_loop) and then created a new timer to call itself again and fire the event again - an infinite loop. I created an instance of this class and added it to the stage (top level object).
I went through my code and replaced all event listeners to use game_loop, not enter_frame. I figured this would work. WRONG. I can't seem to figure how custom events work. When I create the game_loop class on the same "level" as game_loop listener, ONLY that listener can hear it.
stage
|
|-->class1(child of stage)
|
|-->class2 (child of class1)
|
|-->class3 (child of class2)
If i create the game_loop class at the stage level, class1, 2, and 3 don't hear the events being fired. If i create the game_loop class at the class1 level, classes2 and 3 don't hear the events being fired. If i create the game_loop class at class3 level, then class1 and 2 don't hear the events being fired.
Can someone please explain how custom events are meant to work? I am not sure what the expected behavior is, so I do now know if i am doing something wrong!
Thank you for any help, I hope this makes sense!
Comments
check this answer:
http://giderosmobile.com/forum/discussion/4393/stuck-with-simple-custom-event/p1
Additionally you can apply some hacks to broadcast events:
http://giderosmobile.com/forum/discussion/comment/13991#Comment_13991
In my case, I have a child of "target". Would Gun firing an event be heard by a listener in a child of "target"?
I tried using your hack too, however it did not work is there any other way to have global events?
I used a global variable called gameloop and added the event listener to that something like:
gameloop:addEventListener(EVENT_GAME_LOOP, self.onEnterFrame, self)
It seems a bit unclean, but it does work for global event listeners! =P
the idea on global broadcast is that youhave a global table with all listener and you dispatch event to all these listeners
Same way you can simply have a table with all the scenes or object that may want to receive the event, and loop through them to dispatch event to them