开发者

Deal with same method body in different handlers

I have a method fired on mouse down:

private function setGender(e:MouseEvent):void
        {
            check.visible = true;
            check.x = e.target.x;
            check.y开发者_如何学JAVA = 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 MouseEvent and KeyboardEvent. This means an Event is actually what you'd like to use.

private function setGender(e:Event):void
        {
            check.visible = true;
            check.x = e.target.x;
            check.y = e.target.y;
        }

As a base Event has 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.

private function setGender(e:Event):void
        {
            if (e is MouseEvent)
            {
                //handle mouse
            }
            else
            {
                //handle keyboard
            }
            check.visible = true;
            check.x = e.target.x;
            check.y = e.target.y;
        }


You could just use the same event handler for both, but specify the argument type as Event (which is the base class for both event types that you target.) Note that this will of course not work if you are using anything that is specific to KeyboardEvent in the other handler (that you are not showing) but if they are really identical it will work.

private function setGender(e : Event) : void
{
    check.visible = true;
    check.x = e.target.x;
    check.y = e.target.y
}

This event listener can be used for any event that is dispatched by a display object (or any object with x and y properties).


Personally, I think you code would be cleaner and better express your intent if you were to make use of the extract method refactoring pattern as you originally suggested:

private function onMouseEvent(event : MouseEvent) : void { 
    updateGender(event.target);
}

private function onKeyboardEvent(event : KeyboardEvent) : void {
    updateGender(event.target); // are you sure event.target is the intended 
                                // DisplayObject on a KeyboardEvent?
}

private function updateGender(target : DisplayObject) : void {
    // update the DisplayList here.
}

This way you are providing a decoupling between the trigger (either a MouseEvent or a KeyboardEvent) and the reaction (the code in updateGender()).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜