这已经困扰了我一段时间了,我的目标是能够通过textfield将文本写到舞台上(同时会有多个文本字段)。然而,我想要一个按钮能够删除所有的文本在一次。
我有我想要的短信。
基本上,我希望按钮移除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
}
这个阶段不是文本字段的父级吗?我在孩提时就添加了文本字段,所以我看不出为什么不。
这看起来很直截了当,我看不出有什么问题,任何帮助都会很好
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
}
发布于 2016-04-16 23:30:59
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
一样分组。
https://stackoverflow.com/questions/36669234
复制相似问题