首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AS3错误#2025年:提供的DisplayObject必须是调用方的子级。当试图带走一个孩子时

AS3错误#2025年:提供的DisplayObject必须是调用方的子级。当试图带走一个孩子时
EN

Stack Overflow用户
提问于 2016-04-16 20:16:15
回答 2查看 140关注 0票数 0

这已经困扰了我一段时间了,我的目标是能够通过textfield将文本写到舞台上(同时会有多个文本字段)。然而,我想要一个按钮能够删除所有的文本在一次。

我有我想要的短信。

基本上,我希望按钮移除textfield子字段,这样他们就不再被看到了,但是我一直收到这个错误,下面是我的代码:

代码语言:javascript
运行
复制
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
}

这个阶段不是文本字段的父级吗?我在孩提时就添加了文本字段,所以我看不出为什么不。

这看起来很直截了当,我看不出有什么问题,任何帮助都会很好

代码语言:javascript
运行
复制
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
}
EN

Stack Overflow用户

回答已采纳

发布于 2016-04-16 23:30:59

textfield是函数mUp的局部变量。它甚至不存在于函数erase中。

在两个函数之外声明变量。(顺便说一句:永远不要向stage添加任何东西)

代码语言:javascript
运行
复制
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
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36669234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档