【问题标题】:AS3 Error #2025: The supplied DisplayObject must be a child of the caller. When trying to remove a childAS3 错误 #2025:提供的 DisplayObject 必须是调用者的子对象。当试图移除一个孩子时
【发布时间】: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


    【解决方案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
    }
    

    您仍然面临的问题是您在stage 上注册了一个MOUSE_UP 事件,该事件将在您每次松开鼠标按钮时触发。

    这包括点击您的按钮。

    最重要的是单个textfield变量只能容纳一个对象,但您的要求是:

    一次会有多个文本字段

    因此您需要将所有创建的 TextField 对象存储在一个 Array 中,或者以其他方式将它们组合在一起,例如一个常见的 DisplayObjectContainer

    【讨论】:

    • 感谢您的回答,我在 OP 上修改了我的代码。但是它仍然会引发同样的错误,我想我改变了你所说的一切。这是否意味着我将不得不使用数组?我本来希望将它们存储在 DisplayObjectContainer 中,但它似乎不起作用,还是我错过了其他东西?
    • 不用说了,我发现我的clear功能还是看不到child被添加了,我会想办法解决的,谢谢帮助
    • @user3418126 您的项目中还有其他名称为textfield 的内容吗?每次点击按钮都会出现错误吗?您从erase() 中的trace(textfield.parent); 得到什么?
    • 我对文本字段也有 mDown 和 MMove 命令,以便它们可以移动,但我认为这不是问题(这也是添加文本字段子项的唯一部分)。跟踪返回 null 我相信这是因为在 mUp 函数期间添加了子项,但由于我的其余代码是如何编写的,所以我需要添加它。没有简单的解决方案
    【解决方案2】:

    这是一个有效的例子: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
              }
          }
      
      
      }
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2023-03-13
      • 2012-02-29
      • 2012-04-15
      相关资源
      最近更新 更多