这一直困扰着我一段时间,我的目标是能够通过文本字段将文本写到舞台上(一次会有多个文本字段)。但是我希望一个按钮能够一次删除所有文本。
我的文字按照我的意愿运作。
所以基本上我希望按钮删除textfield子项,以便它们不再被看到,但我不断收到此错误,这是我的代码:
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(MouseEvent): void {
var textfield = new TextField();
textfield.type = TextFieldType.INPUT
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;
stage.addChild(textfield); // adding the child here
}
function erase(evt: MouseEvent): void { //triggers when button is clicked
stage.removeChild(textfield) //trying to remove the child, but throws the error
}
舞台不是文本字段的父级吗?我小时候把texfield添加到了它,所以我不明白为什么不这样做。
这看起来非常直接,我没有看到问题,任何帮助都会很好
var board: Sprite = new Sprite(); // displayobjectcontainer to hold the textfields
addChild(board);
var textfield:TextField; // Declared outside the functions
listener.addEventListener(MouseEvent.MOUSE_UP, mUp); // I added an object on the stage to catch my mouse input instead of the stage, so that it doesn't trigger when I click my button
function mUp(evt:MouseEvent):void
{
textfield = new TextField(); // I have this still so it will create a new textfield on every mUP
textfield.type = TextFieldType.INPUT
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;
board.addChild(textfield); // adding the child to the sprite now
}
function erase(evt:MouseEvent):void
{
board.removeChild(textfield) //trying to remove the child, but still throws the error
}
答案 0 :(得分:1)
textfield
是函数mUp
的局部变量。它甚至不存在于函数erase
内。
在两个函数之外声明变量。 (顺便说一句:永远不要向stage
添加任何东西)
var textfield:TextField;
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(evt:MouseEvent):void
{
textfield = new TextField();
textfield.type = TextFieldType.INPUT
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;
addChild(textfield); // adding the child here
}
function erase(evt:MouseEvent):void
{
removeChild(textfield) //trying to remove the child, but throws the error
}
您仍面临的问题是您在MOUSE_UP
上注册了stage
事件,每次您松开鼠标按钮时都会触发该事件。
这包括点击您的按钮。
最重要的是单个textfield
变量只能容纳一个对象的问题,但您的要求是:
一次会有多个文本字段
因此您需要将所有已创建的TextField
个对象存储在Array
中,或者将其组合在一起,就像常见的DisplayObjectContainer
一样。
答案 1 :(得分:0)
这是一个有效的示例:http://wonderfl.net/c/omKl
要恢复:
将您的文本字段放在容器中,将来可以更容易地操作它们(不透明度,位置等)
package {
import flash.text.TextFieldType;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class Main extends Sprite {
private var textsContainer:Sprite;
private var textfieldsList:Array;
public function Main() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected function addedToStageHandler(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
textfieldsList = new Array(); // Array to memorize the textfields
textsContainer = new Sprite(); // Textfields container
addChild(textsContainer);
addEraseButton();
stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
}
// Erase Button
private function addEraseButton():void {
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0xFF0000, 1);
btn.graphics.drawCircle(0, 0, 20);
btn.graphics.endFill();
btn.x = btn.y = 25;
btn.buttonMode = true;
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, eraseBtn_clickHandler);
}
private function eraseBtn_clickHandler(event:MouseEvent):void {
// remove all the textfields memorized in the list
for each(var tf:TextField in textfieldsList) {
textsContainer.removeChild(tf); // remove child from it's parent
}
// reset textfields list
textfieldsList.length = 0;
}
private function stage_mouseUpHandler(event:MouseEvent):void {
if(event.target == stage) { // check if target is stage to only add textfield when needed
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.x = mouseX;
tf.y = mouseY;
tf.border = true;
tf.backgroundColor = 0xCCCCCC;
tf.background = true;
tf.multiline = false;
tf.height = 20;
stage.focus = tf;
tf.selectable = false;
textsContainer.addChild(tf); // add textfield to a container
textfieldsList.push(tf); // memorize the textfield
}
}
}
}