【发布时间】:2016-04-16 20:16:15
【问题描述】:
这一直困扰着我一段时间,我的目标是能够通过文本字段将文本写入舞台(一次会有多个文本字段)。但是我希望一个按钮能够一次删除所有文本。
我的文字可以按我的意愿工作。
所以基本上我希望该按钮删除文本字段子项,以便不再看到它们,但我不断收到此错误,这是我的代码:
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
}
【问题讨论】:
标签: actionscript-3