简体   繁体   English

AS3创建一个“文字工具”

[英]AS3 Creating a “Text Tool”

So i'm developing an application in which allows users to Type on the stage. 因此,我正在开发一个允许用户在舞台上打字的应用程序。 The text needs to appear wherever the mouse cursor is positioned (and the text would follow the mouse cursor if moved). 文本需要显示在鼠标光标所在的任何位置(如果移动,文本将跟随鼠标光标)。 For the most part it works pretty well. 在大多数情况下,它运行良好。 I have two questions. 我有两个问题。

1) How would I allow the text to follow the mouse cursor? 1)如何允许文本跟随鼠标光标? My current code only allows users to type when they click down on the stage. 我当前的代码仅允许用户在舞台上单击时输入。

  var textfield = new TextField();
textfield.type = TextFieldType.INPUT
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textformat;
textfield.textColor = selectedColor;
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;

        /* Add text to stage*/

board.addChild(textfield);

My second question, how would I clear the text? 第二个问题,我将如何清除文本? The text would be "pasted" down onto the canvas, so using the "" method would not work. 文本将被“粘贴”到画布上,因此使用“”方法将不起作用。 I want an "eraser" to erase the text from the stage. 我想要一个“橡皮擦”来擦除舞台上的文字。 It would be just like stage.graphics.clear but just for text. 就像stage.graphics.clear一样,只是用于文本。

Any help I could get would be greatly appreciated. 我能得到的任何帮助将不胜感激。 I am still very new to AS3 and I think I gave all the information needed. 我对AS3还是很陌生,我想我已经提供了所有需要的信息。 Thanks. 谢谢。

To make your text follow the mouse cursor, you can add an event listener to the stage: 要使文本跟随鼠标光标,可以在舞台上添加事件侦听器:

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
function onMouseMove(event:MouseEvent){
  textfield.x = event.stageX;
  textfield.y = event.stageY;
}

to make it stop following, you can remove the event listener: 要使其停止,可以删除事件监听器:

stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

to clear the text, you call textfield.text = ""; 要清除文本,请调用textfield.text = "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM