Answered
MouseEvents on main sprite
This is probably a beginners question, but I have been
puzzled for a few days.
This works as expected (imports ommited):
public class Test extends Sprite {
public function Test() {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(1);
circle.graphics.beginFill(0xFF8000);
circle.graphics.drawCircle(50, 50, 10);
circle.addEventListener(MouseEvent.MOUSE_DOWN, down);
addChild(circle);
}
private function down(evt:MouseEvent):void {
trace("down");
}
}
However if I remove the circle object and directly draw on the main sprite, it doesn't work:
public class Test extends Sprite {
public function Test() {
graphics.lineStyle(1);
graphics.beginFill(0xFF8000);
graphics.drawCircle(50, 50, 10);
addEventListener(MouseEvent.MOUSE_DOWN, down);
}
private function down(evt:MouseEvent):void {
trace("down");
}
}
No matter where I click, I can't get the event to be fired.
I know I can attach the listener to the stage, but I'm wondering why it doesn't work on the sprite directly.
I'm sure there is a perfectly reasonable explanation, I just can't figure it out.
Can any one help?
Thanks in advance,
Peter
This works as expected (imports ommited):
public class Test extends Sprite {
public function Test() {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(1);
circle.graphics.beginFill(0xFF8000);
circle.graphics.drawCircle(50, 50, 10);
circle.addEventListener(MouseEvent.MOUSE_DOWN, down);
addChild(circle);
}
private function down(evt:MouseEvent):void {
trace("down");
}
}
However if I remove the circle object and directly draw on the main sprite, it doesn't work:
public class Test extends Sprite {
public function Test() {
graphics.lineStyle(1);
graphics.beginFill(0xFF8000);
graphics.drawCircle(50, 50, 10);
addEventListener(MouseEvent.MOUSE_DOWN, down);
}
private function down(evt:MouseEvent):void {
trace("down");
}
}
No matter where I click, I can't get the event to be fired.
I know I can attach the listener to the stage, but I'm wondering why it doesn't work on the sprite directly.
I'm sure there is a perfectly reasonable explanation, I just can't figure it out.
Can any one help?
Thanks in advance,
Peter