I have a method fired on mouse down:
private function setGender(e:MouseEvent):void
{
check.visible = true;
check.x = e.target.x;
check.y = e.target.y;
}
The same method body is shared by another method fired on a keyboard Event (so, it takes a keyboard Event for argument). Which is the best way to deal with situations like this one? I would like to have only one method!
One way could be to create a new method called by the two event handlers (even if, if I need to use e.target, it can change according to the type of event and other variables, so I should eventually create a method that takes e.target as a parameter). This creates three methods…
Would it be possible to change for example e:MouseEvent with a parent of MouseEvent and KeyboardEvent? Would it work? Any drawbacks? Is this totally wrong?
Your last sentence is on the right track, but not quite correct. What you actually want is an Event type that is higher in the hierarchical structure than both
MouseEventandKeyboardEvent. This means anEventis actually what you’d like to use.As a base
Eventhas the only property you’re looking for in both events, this should work. If, however, you want to use a different property, even one shared by MouseEvent and KeyboardEvent, you can only use it if it exists in the base Event class. However, you can also try casting the event if you end up wanting to use other properties of the events.