I’m having problem with listening to an event dispatched from a child class, and I don’t know why?
I have to classes:
class 1, witch draws a rectangle and dispatches a custom Event
package
{
import flash.display.;
import flash.events.;
import flash.text.*;
public class clase1 extends Sprite
{
private var labelField:TextField;
public function clase1(label:String = "buttono") {
// draw the background for the button.
graphics.beginFill(0x3366CC);
graphics.drawRect(0, 0, 100, 30);
// store the label as the button’s name.
name=label;
// create a TextField to display the button label.
labelField = new TextField();
// ensure clicks are sent from labelField rather than the button.
labelField.mouseEnabled=false;
labelField.selectable=false;
labelField.text=label;
labelField.x=10;
labelField.y=10;
labelField.width=80;
labelField.height=20;
addChild(labelField);
dispatchEvent(new Event("Hello",true));
}
}
}
class 2, witch draws another rectangle and listens to the event
package {
import flash.display.;
import flash.events.;
import flash.text.*;
public class clase2 extends Sprite {
private var labelField:TextField;
public function clase2(label:String = "buttono") {
// draw the background for the button.
graphics.beginFill(0xFFFCCC);
graphics.drawRect(200, 0, 100, 30);
// store the label as the button’s name.
name=label;
// create a TextField to display the button label.
labelField = new TextField();
// ensure clicks are sent from labelField rather than the button.
labelField.mouseEnabled=false;
labelField.selectable=false;
labelField.text=label;
labelField.x=210;
labelField.y=10;
labelField.width=80;
labelField.height=20;
addChild(labelField);
addEventListener( "Hello",eventHandler,true);
}
function eventHandler( event: Event )
{
trace( "event received ");
}
}
}
and on the fla I have
import clase1;
var c1:clase1 = new clase1();
import clase2;
var c2:clase2 = new clase2();
addChild(c2);
c2.addChild(c1);
, making c2 parent of c1, but no message appears, why??
thankyou
Just a quick glance, it looks like c1 is going to fire the event from it’s constructor before c2 is ever created and adds the event listener. Try firing the event from a different method in c1, and you can call that method after you have called c2.addChild(c1).
If I modify your code just a bit, I am able to see the event be fired
And then in the main application