任何人都可以解释这个代码我如何在我的应用程序中使用它

时间:2013-05-04 08:10:27

标签: actionscript-3 flex flex4.5

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

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应用程序中使用它。

1 个答案:

答案 0 :(得分: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评论中提出的自定义事件说明

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