Skip to main content
May 21, 2008
Answered

MouseEvents on main sprite

  • May 21, 2008
  • 2 replies
  • 354 views
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 topic has been closed for replies.
Correct answer
The problem is you are not trying to listen to an object. A standard procedure is to create the object, attach that object, then listen to that object, but if you create an object, and listen to the internals of what it drew, it cant (graphics is not interactive... but sprite is interactive). The graphics scope inside a Sprite creates an internal "Shape" - the Sprite "has" a shape in it - but it can't be interactive. Anything that extends InteractiveObject can have events... which the Shape object does not. Hope this helps sir :)

2 replies

robdillon
Participating Frequently
May 21, 2008
In your first example, you have created a new sprite and placed it in the variable "circle". Then you attached the event listener to that variable. In your second example, you have simply drawn a circle on the stage. It is not a sprite, it has no reference, it is just a simple stage object. Your event listener is not connected to anything. As ccoonen says above, you can connect the event listener to the stage, but that's not really what you want to achieve.

Your first example is roughly analogous to creating a movieClip on the stage and giving it an instance name of "circe". The second example is like just drawing a circle on the stage and not making it a movieClip.
May 21, 2008
I'm just a bit confused, because, for me, the stage is also a Sprite.

If I do a "graphics.drawCircle(...)" that is exactly the same as "this.graphics.drawCircle(...)" (I think so, correct me if I'm wrong.
So I don't see the difference between this.graphics.drawCircle (where "this" is a Sprite) and circle.graphics.drawCircle (where circle, again, is a Sprite)

It's probably the way things are displayed that makes the difference, the "main" sprite does not do event handling. But for me it seems a bit counterintuitive.
May 21, 2008
Maybe just one more note: I am assuming, that when the flash applet is started, the interpreter will create the Test object and execute the contstructor (function Test). Which means the interpreter will construct a Test sprite and afterwards display it (by doing something comparable to the addChild on the main screen).

Is this correct? Maybe that's not how it works, which would explain why things behave differently for my main sprite and any sprite I construct explicitely myself.
May 21, 2008
package {
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Main extends Sprite {
public function Main() {
graphics.lineStyle(1);
graphics.beginFill(0xFF8000);
graphics.drawCircle(50, 50, 10);
graphics.endFill();
//addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
}

private function down(evt:MouseEvent):void {
trace("down");
}
}
}

I believe you can't listen to your own drawn elements (like you are trying to listen to obj.graphics, not obj - take it a step out and listen to it (for example, listening from the stage works as shown in the example above)