We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Event bubbling/dispatching/listening
Page Index Toggle Pages: 1
Event bubbling/dispatching/listening (Read 608 times)
Event bubbling/dispatching/listening
Sep 17th, 2009, 3:41pm
 
hi, i'm scratching my head over a situation which could be illustrated like this:

there are some classes stacked into each other. a city, with a varying amount of roads, where each of these roads can have a varying amount of both cars and pedestrians. now what needs to happen is:
-if any one of the cars does honk, then any of the pedestrians at the same location in the city as that car, drop their pants.
-The pedestrian does not have to be on the same road as the car, just the same city location.

now the original plan was to funnel the horn signal into the road the car is on, then pass it from there to the city, then ask the city if there is any road on that location, then let the city ask any roads which cross this point if there are any pedestrians there, then let them jump, but this is oh so tedious.

a friend told me there is something in ActionScript called EventListener and EventDispatcher, which would let all pedestrians listen to any horn signals directly and act accordingly, even if they haven't seen a car before ... and there should be something similar in Java right? which would hopefully be not too difficult to use in Processing?

i'm currently trying a workaround using OSC messages, but would be happy if i could do without.
Re: Event bubbling/dispatching/listening
Reply #1 - Sep 18th, 2009, 3:38am
 
AWT has event listeners, but they are not necessarily suited to your needs.
You can do your own quite easily, that's one well known design pattern (Observer). For example, Brian Goetz gives a simple and robust generic implementation.

The idea is that an event source (the car's horn) accepts listeners by offering a standard function to register objects wanting to listen to it. When an object registers, the horn stores the reference to it. When an event arises, the horn iterates on all registered objects and activates their standard event handler.
Page Index Toggle Pages: 1