I am relatively new to Flash and I am trying to make a little game. For that I need to detect, if the player clicked on a plane or a bird.
I am spawning them with addChild and the name of each instance is generated. The eventlistener is attached to the instance.
I tried detecting it like that, but it doesn't seam to work. It detects the clicking (it prints out the shot: instance but not the trace commands in the if), but not was was clicked on.
function shoot(e: MouseEvent): void {
trace("shot: "+ e.target.name);
if (e.target is Plane) {
trace("shot plane");
e.target.parent.removeChild(e.target);
gotoAndStop(3);
}
if (e.target == Bird) {
trace("shot bird");
score += 1;
e.target.parent.removeChild();
}
}
Does anybody have a tip?
Try using e.currentTarget
rather than e.target
.
if (e.currentTarget is Plane) {
...
}
if (e.currentTarget is Bird) {
...
}
The current target of an event is a reference to the item you added the event listener to. The target, on the other hand, is the item actually clicked (which could be the same as current target, or a descendant/child object of it)
You can use getQualifiedClassName
to check the object type:
trace(flash.utils.getQualifiedClassName(e.currentTarget));