Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to dispatch event informing children?

LEGEND ,
Mar 29, 2008 Mar 29, 2008
Hi,

Does anybody know how to dispatch event to all children of parent object? Do
I have to dispatch event for all items in children collection separately?

Regards,
Marek


TOPICS
ActionScript
4.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2008 Mar 30, 2008
Well... you can not "dispatch an event to an specific object", you just "dispatch the event". All objects that have a listener waiting for that event will be notified. Could you tell us more about what you want to do ?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2008 Mar 30, 2008
> Well... you can not "dispatch an event to and specific object", you just
> "dispatch the event".

Yes, I can.

for(var i=0; i<this["pageSelector"].numChildren; i++)
this["pageSelector"].getChildAt(i).dispatchEvent(event);

If I dispatch the event directly - just calling dispatchEvent() in current
object then none of child objects will recevie it. That is my problem. Event
definition with bubbling set to true or false nothing changes. It seems that
event always goes up - never down.

> All objects that have a listener waiting for that event
> will be notified. Could you tell us more about what you want to do ?

Ok. Gallery cosist of couple of pages so I need the page selector object.
This object consist of couple of items representing each page number. Each
item has 2 states: active (current page) and inactive. When item is clicked
I would like to:

1. inform the master object to change the page of results
2. inactivate other page items then selected

It is easy to dispatch event on item level and bubble it up to master object
but the problem is to inform other items to set them to inactive state.
Example above is temporary work around to achieve it. Notice that target
event phase is triggered on source item only.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2008 Mar 30, 2008
quote:


>> Well... you can not "dispatch an event to and specific object", you just
>> "dispatch the event".

>Yes, I can.

>for(var i=0; i<this["pageSelector"].numChildren; i++)
> this["pageSelector"].getChildAt(i).dispatchEvent(event);




Hmm... not really Marek.
In your code, what you're doing is not dispatching the event to a specific object, instead, you are dispatching the event from a specific object. In this case you're dispatching the event from the DisplayObject you got from >> this["pageSelector"].getChildAt(i) << so any objects that have a listener waiting for the event dispatched from >> this["pageSelector"].getChildAt(i) << will be notified.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2008 Mar 30, 2008
Hi Albert,

> Hmm... not really Marek.
> In your code, what you're doing is not dispatching the event to a specific
> object, instead, you are dispatching the event from a specific object.

Yes, that's true. This is what I meant. My intention was to avoid such a
strange construction. I would prefer to dispatch only one event in parent
object and force something like capturing mode but through the all objects
down the parent. As I mantioned: I would like to create something like
radiobuttons where each button must be informed that another one has been
clicked and button must be set to inactive state.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 30, 2008 Mar 30, 2008
try adding the event listener to the object you're sending from, but set the callback function to the function you want called in the child object. like this:

//this is inside parent
addEventListener(Event.SOME_EVENT, child.myCallbackFunction);
//this is inside child
function myCallbackFunction(){etc}

not exactly the same thing, but a possible workaround. on the other hand, if the objects you want to send events to are children of the object you are generating events from, that means you already have references to them that you can use to call the functions directly.

in the end it doesnt seem like there *is* a way to just broadcast a message downstream... which is a bummer because that seems really useful

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2008 Mar 30, 2008
> try adding the event listener to the object you're sending from, but set
> the
> callback function to the function you want called in the child object.
> like
> this:
>
> //this is inside parent
> addEventListener(Event.SOME_EVENT, child.myCallbackFunction);
> //this is inside child
> function myCallbackFunction(){etc}

Hmm... but in this case only single child is informed about event. I must
walk through all children in loop. Now I'm using strange construction where
the effect is similar to yours proposal:

for(var i=0; i<this["pageSelector"].numChildren; i++)
this["pageSelector"].getChildAt(i).dispatchEvent(event);

In this case partent object must "know" construction of child object which
is very bad programming conception. The best solution would be trigger
"magic" event which will be propagated down the object chierarchy (something
like capturing mode but not limited to source object and its parents).
Logically - this is very basic situation where parent object wants to send
event to all children. So it should be possible using standard Flash
functionality of dispatching events. Do I have to create my own event flow
model for this kind of events?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2008 Mar 30, 2008
Ok ok ok. sorry Marek.
I have another solution. It probably helps you.
Why don't you put a listener to the parent object. (the parent of all you're radio buttons). In the listener function you get the element clicked with the "target" property instead of "currentTarget". This is for being able to know exactly what object inside the parent was clicked. Also in the listener function you disable all your buttons but the clicked button.

something like...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2008 Mar 31, 2008
LATEST
Hi Albert,

You are almost close to my intentions :-))

> parentClip.addEventListener(MouseEvent.CLICK, parentListenerFunction);
>
> function parentListenerFunction(event:MouseEvent):void
> {
> // disable all buttons here and then...
> MovieClip(event.target).gotoAndStop("enable");
> }

This function allows me to enable child button. Easier is just detect clicks
directly in child. Enabling this is no problem, disablilng ...is. Fuction
should walk throug all children:

function parentListenerFunction(event:MouseEvent):void
{
// disable all buttons here and then... (this thread we should discuss)
for (var i=0; i<numChildren; i++)
{
var child=childAt(i);

if (child==event.target) continue;
if (instanceof(child)!="radioButtonClassByMarek") continue; //I dont
remember name of operator but I most likely understand my intention
child.setDisableState();
}

// MovieClip(event.target).gotoAndStop("enable"); - not needed
}

Of course, I have to add setDisableState() to each button. Sounds great
but... this is old style programming - without using events. It woulb be
simplier call one dispatchEvent to inform many attached objects if possible.
It seems that current version of Flash not supports this kind of events. Am
I right?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines