使用鼠标移动在Flex中移动富文本。

3
我有一个带有背景的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个回答

2
尝试使用Sprite类中定义的startDrag()和stopDrag()方法:

startDrag()stopDrag()

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

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

谢谢@Pan,它正在工作...但我有一个问题,我在两个矩形之间画了一条连接线,而这个richText位于该线的中间...你知道如何将该线与此richText一起移动吗? - samira
尝试将richText和line放入像sprite、canvas等容器中,然后在容器上添加鼠标事件,就像上面的操作一样。 - Pan
抱歉 @Pan,我又遇到问题了:如果我使用Canvas,因为我的线条是Shape类型,当我使用myCanvas.addChild(myLine)时会出现运行时错误;而如果我使用Sprite,则即使使用mySprite.addChild(richText),也无法显示我的richText!这不奇怪吗?! - samira
1
如果您使用画布,可以像这样添加形状:myCanvas.rawChildren.addChild(myLine); - Pan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接