在flex中移动带有鼠标移动的richtext

时间:2013-06-20 07:59:35

标签: actionscript-3 flex events flex4 mousemove

我有一个带背景的richText,我想用鼠标移动来移动它

所以我写下面的代码:( richText是一个全局字段)

public function createRichText(textPoint:Point):RichText {
richText = new RichText();  
    var measure:String = correlationMeasure.toFixed(4).toString();
    richText.text = measure;
    richText.x = textPoint.x; 
    richText.y = textPoint.y; 
    richText.width = 60;
    richText.height = 24;
    richText.setStyle("fontSize", 11);
    richText.setStyle("horizontalCenter", "0");
    richText.setStyle("verticalCenter", "1");
    richText.setStyle("left", "2");
    richText.setStyle("right", "2");
    richText.setStyle("top", "5");
    richText.setStyle("bottom", "5");
    richText.setStyle("textAlign", "center");
    richText.setStyle("verticalAlign", "middle");
    richText.setStyle("backgroundColor", 0xe6e91f);
    richText.setStyle("backgroundAlpha", 1);
        richText.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 10);
        richText.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 10);
return richText
}


private function mouseDownHandler(e:MouseEvent):void {
    e.stopPropagation();
    allowDraging = true;
    richText.addEventListener(MouseEvent.MOUSE_MOVE, dragWithMouse);
}
private function dragWithMouse(e:MouseEvent):void {
    if (allowDraging) {
        e.stopPropagation();
        var temp:Point = richText.contentToGlobal(new Point(e.localX, e.localY));
        mousePoint = rootComponent.globalToLocal(temp);

        removeChild(richText);
        addChild(createRichText(mousePoint));
        e.updateAfterEvent();*/
    }
}

但它无法正常工作...意味着它不会随着鼠标移动而连续移动并且已经跳跃!有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

尝试Sprite类中定义的startDrag()stopDrag()方法:

  private function mouseUpHandler(e:MouseEvent):void {
      e.stopPropagation();
      richText.stopDrag();
  }

  private function mouseDownHandler(e:MouseEvent):void {
       e.stopPropagation();
       richText.startDrag();
 }
相关问题