PDA

View Full Version : Dispatch an event from root which should received by childs.



messeb
May 9th, 2007, 01:55 PM
Hi,

I have a class, which extends flash.display.Sprite.


package {
import flash.display.Sprite;
import flash.events.Event;

public class Circ extends Sprite {

public function Circ() {
init();
this.addEventListener("MainEvent", onMainEvent);
}
public function init():void {

var tmpCol:uint = Math.random()*16000000;
var tmpX:uint = Math.random()*400+50;
var tmpY:uint = Math.random()*200+50;

this.graphics.lineStyle(1);
this.graphics.beginFill(tmpCol);
this.graphics.drawCircle(tmpX, tmpY, 50);
this.graphics.endFill();
}
public function onMainEvent(e:Event):void {
trace("MainEvent occurs!");
}
}
}

These elements (Circ) will be added to the main timeline and should be listen
to the Event "MainEvent".
I will dispatch an event (flash.events.Event) from root which should be
received by every Circ instance (children of root) on the stage.


/**
* first frame timeline
*/
import flash.events.Event;

for (var i:uint = 0; i < 5; i++) {
this.addChild(new Circ());
}
this.dispatchEvent(new Event("MainEvent"));


I will that every Circ instance make the output "MainEvent occurs!".

But this doesn't work.
Could anyone help?

senocular
May 9th, 2007, 02:07 PM
you have to use addEventListener with root since that is the instance dispatching the event. Right now you have the Circ class only listening to itself which will only recognize events coming from itself

Rezmason
May 9th, 2007, 02:20 PM
Events don't work that way. Sorry, bub. :)

What your class is doing right now is it's listening to itself for a "MainEvent". Instead you're going to want them to listen to your main object for the MainEvent.

I'd do it like this:

(in your class)


mainObject.addEventListener("MainEvent", onMainEvent);


(and in your mainObject, whatever it is)


this.dispatchEvent(new Event("MainEvent"));


You'll need to make sure that mainObject, whatever it is (for instance it could be this.stage), is within the scope of your class's code. You could do that by passing a reference to the mainObject when you make a new instance of your class:



this.addChild(new Circ(this));


...and then you'd program Circ's constructor to accept the parameter.

EDIT: beaten to the punch by Sen. :P

messeb
May 10th, 2007, 03:20 AM
Thanx, I understand.

But can I do this WITHOUT passing a reference.

For example the event MouseEvent.CLICK goes automatical deeper in the hierarchy of the DisplayList, if I click on a movieclip/button which is in an other movieclip.

Example:

stage
-> movieclip
- -> button



import flash.display.Sprite;
import flash.events.MouseEvent;

function onChild(e:MouseEvent):void {
trace("child");
}
function onParent(e:MouseEvent):void {
trace("parent");
}
function onStage(e:MouseEvent):void {
trace("stage");
}

var child:Sprite = new Sprite();
child.graphics.lineStyle(1);
child.graphics.beginFill(0x00FF00);
child.graphics.drawCircle(100, 100, 50);
child.graphics.endFill();

var childParent:Sprite = new Sprite();

childParent.addChild(child);
addChild(childParent);

child.addEventListener(MouseEvent.CLICK, onChild);
childParent.addEventListener(MouseEvent.CLICK, onParent);
addEventListener(MouseEvent.CLICK, onStage);


The output:
child
parent
stage

Can I do something like that with own events?
So, that if I dispatch an event on root, that every child, and there childs will receive the event automatical, without passing a reference to "dispatcher".

Or is this only a specific MouseEvent reaction, because the FlashPlayer dispatch the MouseEvent?

senocular
May 10th, 2007, 07:52 AM
it only goes up, not down. A child will pass its propagated events to a parent. Parents do not pass their events to their children. Chilren-up works because if you click on a child you are also clicking on a parent (if you click on a button in a window, you are also clicking that window since the button makes up that window. However, clicking no where on the stage means your clicking the stage, but doesn't mean you're clicking all of the stage's children).

messeb
May 10th, 2007, 08:17 AM
Many thanks for the explanation.