AS3错误#2025:提供的DisplayObject必须是调用者的子级。当试图移除一个孩子

时间:2016-04-16 20:16:15

标签: actionscript-3

这一直困扰着我一段时间,我的目标是能够通过文本字段将文本写到舞台上(一次会有多个文本字段)。但是我希望一个按钮能够一次删除所有文本。

我的文字按照我的意愿运作。

所以基本上我希望按钮删除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
}

2 个答案:

答案 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

要恢复:

  • 不要直接在舞台上添加元素,而是在主文档或容器上添加元素
  • 在添加一个
  • 时,记住数组中的文本字段(或在Vector中更好。)
  • 将您的文本字段放在容器中,将来可以更容易地操作它们(不透明度,位置等)

    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
            }
        }
    
    
    }
    }