0

我有一个类似这个问题的问题,但我无法理解这个答案代码,任何人都可以解释这个代码。

private function attachListeners():void
{
    this.addEventListener(MouseEvent.MOUSE_DOWN, selfMouseDownHandler, false,0,true);
    this.addEventListener(MoveEvent.MOVE, selfMoveHandler, false,0,true);
}

private function selfMoveHandler(event:MoveEvent):void
{
    redrawConnectedLinks();
}

private function selfMouseDownHandler(event:MouseEvent):void
{
    stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler, false,0,true);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler, false,0,true);
}

private function stageMouseUpHandler(event:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler, false);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler, false);
}

private function stageMouseMoveHandler(event:MouseEvent):void
{
    dispatchEvent(new MoveEvent(MoveEvent.MOVE));
}

请建议我如何在 flex 应用程序中使用它。

4

1 回答 1

0

此代码通过 Flash 的 MouseEvent.MOUSE_UP、MouseEvent.MOUSE_MOVE 和 MouseEvent.MOUSE_DOWN 跟踪鼠标移动。除了这些事件,代码还使用了一个自定义事件:MoveEvent.MOVE。

任何时候,在调度 MoveEvent.MOVE 时,应用程序都会重绘连接器。因此,MoveEvent.MOVE 的侦听器在一开始就被一劳永逸地添加。

此外,为了跟踪拖动,此代码等待 MOUSE_DOWN,然后继续为每个 MouseEvent.MOUSE_MOVE 调度 MoveEvent.MOVE,直到 MOUSE_UP 事件发生。

现在,让我将上面的解释与代码行联系起来:

private function attachListeners():void
{
    /*Wait for mouse down event to initiate mouse move and mouse up tracking*/
    this.addEventListener(MouseEvent.MOUSE_DOWN, selfMouseDownHandler, false,0,true);

    /*Always track MoveEvent.MOVE and start redrawing connectors whenever it's dispatched*/
    this.addEventListener(MoveEvent.MOVE, selfMoveHandler, false,0,true);
}

private function selfMoveHandler(event:MoveEvent):void
{
    /*Redraw connectors when the custom event MoveEvent.MOVE is received*/
    redrawConnectedLinks();
}

private function selfMouseDownHandler(event:MouseEvent):void
{
    /*Wait for mouse up event on receiving a mouse down*/
    stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler, false,0,true);
    /*Track mouse movement once mouse is down*/
    stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler, false,0,true);
}

private function stageMouseUpHandler(event:MouseEvent):void
{
    /*Once mouse up happens stop tracking mouse up... */
    stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler, false);
    /*Don't track mouse movement once mouse button gets released*/
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler, false);
}

private function stageMouseMoveHandler(event:MouseEvent):void
{
    /*dispatch MoveEvent.MOVE when mouse is moved.
      If you read the above setup again, this should get dispatched on mouse moves
      happening after a mouse down and before a mouse up
     */
    dispatchEvent(new MoveEvent(MoveEvent.MOVE));
}

HTH。

OP 在评论中询问的自定义事件的解释

自定义事件对于添加自定义消息传递语义很有用。例如,在这种情况下,鼠标拖动是由一系列鼠标事件检测到的,一旦这些事件发生,就会调度一个自定义事件来通知连接器应该被重绘。同样,您可能有一个刷新按钮,单击它可能会调度相同的事件以强制重绘。

于 2013-05-04T08:48:36.953 回答