Reputation: 49
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? 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.
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. Thanks.
Upvotes: 0
Views: 85
Reputation: 2129
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 = "";
Upvotes: 3