View Full Version : eventDispatcher help please !
sebacuello
May 31st, 2007, 06:09 PM
i have lets say 20 mc on the stage that inherits a custom class. added to stage via addChild.
the class has its constructor function that ads listeners. one to mousevents and other to custom event. i need that when i click on one of these mcs, everyone of them get the custom event and perform an action defines by a function.
is that posible?
i used to do that in as2 with asbroadcaster
plz help on this situation.
Dazzer
June 1st, 2007, 01:22 AM
if you have the listener added inside the constructor, then all your instances of that class will have that functionality. I don't see what's the problem?
sebacuello
June 1st, 2007, 01:18 PM
hi, thanks for the answer. as i see it it should work. in fact, the event triggers the listener inside the mc. but it doesnt propagate to the others mcs.
so i dont know what to do.
these are my classes:
marker.as
package {
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import flash.events.EventDispatcher;
public class marker extends global {
public function marker(ix:Number,iy:Number) {
x=ix;
y=iy;
dispatcher.addEventListener("action", actionHandler);
addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
dispatcher.dispatchEvent(action);
//trace(event.currentTarget.parent);
}
addEventListener(Event.ENTER_FRAME, handlerEnterFrame);
function handlerEnterFrame(event:Event) {
//trace(event.target);
}
}
function actionHandler(event:Event):void {
trace(this);
trace("actionHandler: " + event);
x+=10;
}
}
}
global.as
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
public class global extends MovieClip {
public var action:Event=new Event("action");
public var dispatcher:EventDispatcher = new EventDispatcher();
public function global() {
}
}
}
and this is how they are instantiated in the .fla
var markers:global=new global();
addChild(markers);
var mrk1:marker=new marker(125,124);
var mrk2:marker=new marker(199,124);
var mrk3:marker=new marker(125,188);
var mrk4:marker=new marker(122,168);
markers.addChild(mrk1);
markers.addChild(mrk2);
markers.addChild(mrk3);
markers.addChild(mrk4);
Huinoio
June 2nd, 2007, 10:09 AM
Could it be that 3rd parameter in addEventHandler, which is false by default?
Maybe try dispatcher.addEventListener("action", actionHandler,true);
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.